Did you ever want to use builders in your grails taglib and wondered why the approach mentioned in the grails documentation doesn’t work at all? Here is what I found out after a lot of trial and error and digging in the MarkupBuilder, BuilderSupport and GroovyPagTagBody sources. Let’s stick with the example from the documentation and correct it until it works (the impatient can scroll down):
-
def dialog =
{ attrs, body ->
-
def markup =
new groovy.
xml.
MarkupBuilder(out
)
-
markup {
-
div(‘class’: ‘dialog’) {
-
body()
-
} } }
>> more…
We are using Groovy on Grails for an incubator project, which is incredibly refreshing! Groovy as a dynamic language with optional type annotations doesn’t quite rock my boat though. With static type checking the types you define are an essential element of design. I like to point out that Groovy’s creator wrote:
“I can honestly say if someone had shown me the Programming in Scala book by by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I’d probably have never created Groovy.” >> more…
One of the biggest advantages of Groovy over Java are those nice little collection helpers. For people who first encountered them in functional languages the names are confusing. Here’s a short comparison:
| Functional Language |
Groovy |
|
| map (list, function) |
list.collect (closure) |
Applies the function to each element of the list and returns the results as a new list |
| filter (list, function) |
list.findAll (closure) |
Returns a list of all elements in the input list for which the function returns true |
| fold (list, initial element, function) |
list.inject (initial element, closure) |
Applies the function to the initial element and the first element of the list. Then applies the function to this result and the second element of the list. Get the idea? |
>> more…