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 = {
def results = Task.list(max: 10, sort: ‘id’, order: ‘desc’)
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}”)
pubDate(new Date()) // might want to put something useful in here
}
}
}
}
}
}
[/groovy]
Which will result in something like this:
[xml]
create a simple RSS feed
[/xml]
Wonderful isn’t it?
Apart from this, it might be useful to know that Grails has build-in builders for JSon as well!
nice example, thanks! but it only works 90%, because link() errors with StackOverflow:
http://markmail.org/message/msonhlrlpumtbi42
Thanks for pointing this out! At the time I wrote this blog the above worked; I’ll update it!