Grails - Render method & Markup builder

During Graemes' demo of Grails at javasummercamp he briefly pointed out the fact that one can implicitly use the Groovy MarkupBuilder (Similar to the Markaby DSL for Ruby) within the render method of an action for generating markup.

Which makes writing an RSS feed for you application really easy:

GROOVY:
  1. def feed = {
  2.   def results = Task.list(max: 10, sort: 'id', order: 'desc')
  3.   render(contentType: 'application/rss+xml') {
  4.       rss(version: '2.0') {
  5.           channel{
  6.               title('recent tasks')
  7.               description('A feed of the 10 most recent tasks')
  8.               link('/task/list')
  9.               results.each { task ->
  10.                   item{
  11.                       title(task.subject)
  12.                       description(task.description)
  13.                       link("/task/view/${task.id}")
  14.                       pubDate(new Date()) // might want to put something useful in here
  15.                   }
  16.               }
  17.           }
  18.       }
  19.   }
  20. }

Which will result in something like this:

XML:
  1. <rss version='2.0'>
  2.   <channel>
  3.     <title>recent tasks</title>
  4.     <description>A feed of the 10 most recent tasks</description>
  5.     <link>/task/list</link>
  6.     <item>
  7.       <title>Create RSS feed with the markup builder</title>
  8.       <description>After investigating the markupbuilder, use it to
  9.       create a simple RSS feed</description>
  10.       <link>/task/view/2</link>
  11.       <pubDate>Sun Aug 26 15:06:43 CEST 2007</pubDate>
  12.     </item>
  13.     <item>
  14.       <title>Investigate MarkupBuilder</title>
  15.       <description>Investigate the MarkupBuilder</description>
  16.       <link>/task/view/1</link>
  17.       <pubDate>Sun Aug 26 15:06:43 CEST 2007</pubDate>
  18.     </item>
  19.   </channel>
  20. </rss>

Wonderful isn't it?

Apart from this, it might be useful to know that Grails has build-in builders for JSon as well!


0 Responses to “Grails - Render method & Markup builder”

  1. No Comments

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


Tim Bray introducing the (J)Ruby panel Stage being build in the nearby park Community One Keynote Greenland Okke en Rudie pub javaone 2008 goodybag Stretched Limo Moscone Center - JavaOne Acme Anvile at CommunityOne Keynote Hotel room Rudie nearby hotel sea_lion Cable Car line Charles Nutter & Guillaume Laforge Java + You on a cab smashmouth Scribbled Sun Logo Golden Gate
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.