Grails - Render method & Markup builder
Published by peter August 26th, 2007 in grails, groovy, java.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:
-
def feed = {
-
render(contentType: 'application/rss+xml') {
-
rss(version: '2.0') {
-
channel{
-
title('recent tasks')
-
description('A feed of the 10 most recent tasks')
-
link('/task/list')
-
results.each { task ->
-
item{
-
title(task.subject)
-
description(task.description)
-
link("/task/view/${task.id}")
-
}
-
}
-
}
-
}
-
}
-
}
Which will result in something like this:
XML:
-
<rss version='2.0'>
-
<channel>
-
<title>recent tasks</title>
-
<description>A feed of the 10 most recent tasks</description>
-
<link>/task/list</link>
-
<item>
-
<title>Create RSS feed with the markup builder</title>
-
<description>After investigating the markupbuilder, use it to
-
create a simple RSS feed</description>
-
<link>/task/view/2</link>
-
<pubDate>Sun Aug 26 15:06:43 CEST 2007</pubDate>
-
</item>
-
<item>
-
<title>Investigate MarkupBuilder</title>
-
<description>Investigate the MarkupBuilder</description>
-
<link>/task/view/1</link>
-
<pubDate>Sun Aug 26 15:06:43 CEST 2007</pubDate>
-
</item>
-
</channel>
-
</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”
Please Wait
Leave a Reply