Squiz Matrix Simple JSON Helper Snippet
Thu, Jul. 21, 2011When 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" />'} </script>
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:'http://www.zedsaid.com/blog'} </script>
You can then easily reference the page object using normal dot notation:
alert('Page Name: '+page.name+' ID: '+page.id+' URL: '+page.url);
Comments