Condititional Keyword for Last Position Format


(Tbaatar) #1

Matrix Version: 5.5.5.1

Hi,

I’m trying to create JSON file which checks if there is Geo data and prints the result. It works fine but for the last position format it doesn’t work therefore it prints the comma (,) and breaks the JSON format.

Not sure if this is a bug or I’m doing something wrong.

Thanks.

Here is the default format:

    %begin_asset_metadata_tagGeoLongitude%
    {
    "type": "Feature",
    "geometry": {
      "type": "Point",
      "coordinates": [
       %asset_metadata_tagGeoLongitude%,
       %asset_metadata_tagGeoLatitude%
      ]
    },
    "properties": {
      "name": "%asset_name%",
      "description": "%asset_metadata_description%",
      "image": "%asset_metadata_image^as_asset:image_v_small_url%",
      "region": "%asset_metadata_tagRegion^replace:-: ^titleize%",
      "province": "%asset_metadata_tagProvince^replace:-: ^titleize%",
      "link": "%asset_url%"
    }
    },
    %else_asset%
    %end_asset%

and here is the last position format

%begin_asset_metadata_tagGeoLongitude%
{
"type": "Feature",
"geometry": {
  "type": "Point",
  "coordinates": [
   %asset_metadata_tagGeoLongitude%,
   %asset_metadata_tagGeoLatitude%
  ]
},
"properties": {
  "name": "%asset_name%",
  "description": "%asset_metadata_description%",
  "image": "%asset_metadata_image^as_asset:image_v_small_url%",
  "region": "%asset_metadata_tagRegion^replace:-: ^titleize%",
  "province": "%asset_metadata_tagProvince^replace:-: ^titleize%",
  "link": "%asset_url%"
}
}
%else_asset%
%end_asset%

(Iain Simmons) #2

If the item in the last position doesn’t have that metadata field, it would print nothing and therefore there would still be the trailing comma from the previous item.

Since each item may or may not be printed, I think you’d need to handle the trailing comma in the Page Contents bodycopy:

%asset_contents^replace:,\s*$:%

(Tbaatar) #3

Hi Iain,

Thanks for the hint, it works.

This is how my code looks for the Asset Listing page contents:

{
"type": "FeatureCollection",
"features": [
            %asset_listing^replace:,\s*$:%
        ]

}


(Iain Simmons) #4

Great! I’m glad it solved your problem.

Depending on how the assets are organised and what version of Matrix you’re using, it might also be possible to use a loop in server side JavaScript (SSJS) instead.


(Nick Papadatos) #5

I think I remember doing something similar ie; remove last trailing comma.
And If I remember correctly this is what I did (another way of Ians’s solution)

1: In the lister’s new position format you add -1 (this will add another container)
2: copy all you code to that container and delete default format
3: move position -1 Format and create link in the default format folder. That way you only need to make changes in one place. Happy days

If you’re already done you can keep this in you back pocket for another time :slight_smile:

Cheers
Nick