Asset Listings and RESTful URLs


(David Jessup) #1

I'm new to Matrix, and I'm trying to get my head around it by diving in head-first and attempting a little test project. So far I've been somewhat successful, but I've hit a few roadblocks that I'm hoping you gurus here might be able to give me a steer on.


Scenario

I have an external web application that generates a simple JSON feed consisting of an array of ~20 objects, each two properties ("text" and "href"), and some also have a third optional property called "collection". I need to be able to retrieve this feed, combine it with additional data sources (publishing in the same format), select a sub-set of this aggregated collection, then publish this subset as a formatted HTML widget, or as re-published JSON data.



I want the publishing to use RESTful URLs, for example http://example.com/aggregate should output the HTML widget, whereas http://example.com/aggregate.json should output the JSON version.



My attempt

The first bit was straightfoward, I've got a JSON Data Source asset which is linked to a REST Resource which retrieves the JSON feed from my external application and is configured to extract the contents into a bunch of Data Source Record Sets (DSRS). I've then got a second JSON Data Source which is linked up to a Text File asset containing JSON, to simulate the other feed which is being aggregated.



These two JSON Data Sources are then in turn passed into an Asset Listing which is configured to pick 6 random DSRSs from that collection. This is all working fine, and as a simple test of the HTML widget publishing I have it outputting as a <ul> with each DSRS appearing as an <li>. So far so good.



So to add the RESTful JSON URL I configured an alternative context which matches URLs ending in .json and added a corresponding Web Path to my Asset Listing. I then edited (in this new context) the Page Contents and Default Type Format to output JSON. This almost works, except for a couple of small gotcha's explained below.



Problem one

When outputting in JSON, trailing commas are a problem (e.g. after the last property of an object, or the last item in an array). This means that if I simply create an asset listing with something like -



Page Contents:

    {"feed": [
       %asset_listing% 
    ]}

Type Format:

    {
       "text":"%ds__text%",
       "href":"%ds__href%"
    },

then I end up with a malformed output like this,

    {"feed": [
     {
       …
     },
     {
       …
     },  // <-- extra comma
    ]}

To solve this I tried to use a keyword modifier along the lines of %asset_position^neq:6:,: % (where "6" is the last asset position) but this doesn't seem to work at all, and still outputs a comma every time. I've had it output %asset_position% to verify I'm supplying a value that really exists, but it just doesn't seem to work as I believe it's suppose to.



Problem two

I can't seem to find a way to associate designs with specific contexts. I have a design with a parse file which outputs an application/json Content-type header, but I can't find a way to serve the correct design based on the context. It's odd, because under Settings on the Asset Listing, Matrix lists both of the URLs (the regular URL and the .json URL) separately when I apply a design, but I don't seem to be able to update the design for each URL independently?



Please help!

I've gone over the manuals and forum posts as best I can. It feels like all the bricks are here, but I'm missing the cement to put them together. I may have misunderstood some basic Matrix concepts, so feel free to enlighten me or suggest alternatives. I'd like to keep the implementation as DRY as possible, which is why I haven't just created two Asset Listings with different URLs (for example).



I look forward to hearing what you might have to say and suggest.


(Uoebusiness) #2

Re your comma issue. This solution isn't appropriate in all cases but I put the comma first

    ,{
    title: "%asset_name^escapequotes% @ %asset_metadata_event_room% %asset_metadata_event_location%",
    start: "%event_start_datetime_c%",
    end: "%event_end_datetime_c%",
    allDay: false,
    url: "/schoolbiz/documents/?category_id=0&a=%asset_assetid%"
    }


And then put a dummy record in as the first entry.

    
    


Couldn't think of another way around this.

(David Jessup) #3

[quote]
Re your comma issue. This solution isn't appropriate in all cases but I put the comma first



And then put a dummy record in as the first entry.



Couldn't think of another way around this.

[/quote]



Thanks, that hadn't occurred to me as a workaround, so it's good to know!



I don't suppose anyone could shed any light on why %asset_position^neq:<index>:,: % doesn't seem to work as advertised?


(Nic Hubbard) #4

[quote]
Thanks, that hadn't occurred to me as a workaround, so it's good to know!



I don't suppose anyone could shed any light on why %asset_position^neq:<index>:,: % doesn't seem to work as advertised?

[/quote]



Just create a new Position Format, and set it to -1, this will create a new type format for the last item of your listing, which you can remove the comma from. I also just wrote a blog post about this: http://www.zedsaid.com/blog/squiz-matrix-asset-listing-last-position-custom-formatting



Hope it helps.


(David Jessup) #5

[quote]
Just create a new Position Format, and set it to -1, this will create a new type format for the last item of your listing, which you can remove the comma from. I also just wrote a blog post about this: http://www.zedsaid.com/blog/squiz-matrix-asset-listing-last-position-custom-formatting



Hope it helps.

[/quote]



Thanks Nic, that certainly did help - I've now got the comma issue resolved, but solve problem and you find another :rolleyes:



Now I essentially have two copies of the same assets (except one has a comma after it). I've been playing around with snippets, nested content, and %globals_asset_content_raw:XXX%, but none of these seem to process the %ds__XXX% keywords the Asset Listing passes in. I tried to pass them on as GET variables on the nested content, but either I had is misconfigured or this isn't possible. Is this even possible?



Also, I'd greatly appreciate your thoughts on why the %asset_position^neq:X:,: % didn't work in the first place - I'm tempted to file a bug report, but I'd like to make sure I'm not just misusing/understanding the concept.



Update: I think I've found a good solution (probably a "well duh!" for many of you), and that's just to split the delimiter (comma) and content into two DIVs, then link the content DIV to the Position Format rather than re-create it.



Now I just have to figure out how to use a particular design depending on the context…