Dave Johnson on open web technologies, social software and Java
One question that came up recently on the Roller mailing lists was how to generate JSON for a Roller blog. Roller 3.0's new rendering system makes it easy to generate just about any representation of your blog, so JSON is no problem at all. In fact, any Roller user can do it via the Roller UI by simply creating a new page template.
For example, what if you wanted to generate a JSON array of blog entries with id, pubTime and title for each entry, like what's shown below:
title: "Open source ghetto at JavaOne?"},
title: "iPhone: don't think of it as a computer"},
title: "iPhone: Apple apps only?"}
To do that, you can use the Roller Preferences->Template menu to create a page template like so:
#set($pager = $model.getWeblogEntriesPager())
#set($map = $pager.getEntries())
#foreach($day in $map.keySet())
#set($entries = $map.get($day))
#foreach($entry in $entries)
The code above is a little tricky because of the way the entry pager returns entries. To make it easy to display entries by day, the $pager.getEntries() method returns entries in a ordered java.util.Hashmap. The map contains lists of entries, one for each day, and the map is keyed by date objects. To get entries out of the pager you must iterate through the day-date keys, get then entry list for each and then iterate through the entries of that day.
If you create the above template and save it with the name "jsontest" your new page will be available at /<bloghandle>/page/jsontest. And because the $pager object understands the standard Roller request parameters date, cat and page you can subset the data by date and category, and you can page through the results. For example:
/<handle>/page/jsontest?cat=roller - latest entries in category 'roller'
/<handle>/page/jsontest?cat=roller?page=1 - first page of entries in category 'roller'
/<handle>/page/jsontest?date=200601 - Entries from January 2006
Try it on my blog: http://rollerweblogger.org/roller/page/jsontest
For more information on Roller page template programming see the docs for the Apache Velocity template language and see the Roller 3.0 Template Author Guide for details on the Roller objects and macros available within Roller templates.
This is just one entry in the weblog Blogging Roller. You may want to visit the main page of the weblog
Below are the most recent entries in the category General, some may be related to this entry.