Squiz Matrix Simple JSON Helper Snippet

Thu, Jul. 21, 2011

When first setting up a Design in Squiz Matrix I have started to add a little helper JSON snippet so that I can always reference the current page's info in Javascript if I ever need to. It uses simple MySource design tags along with JSON and is extremely helpful. Just add the following to the bottom of your Design parse file:

<script type="text/javascript">
var page = {id:'<MySource_print var="assetid" />',name:'<mysource_print var="asset_name" escape="yes" />',url:'<mysource_print var="asset_url" escape="yes" />'}

Note the use of escape="yes", this is important because we don't want to break our javascript if a name or URL containted quotes.

Once added, it will look something like this:

<script type="text/javascript">
var page = {id:'86',name:'Blog',url:''}

You can then easily reference the page object using normal dot notation:

alert('Page Name: '' ID: '' URL: '+page.url);
Nicholas Hubbard


Robin Shi
Jul 21, 2011
I reckon keyword modifier can escape quotes but you need to use asset listing.
Nicholas Hubbard
Jul 22, 2011
Yeah, that is always an option, but I think for the snippet provided, using design tags is best because we are working in the parse file.

Add Comment