Monday, February 21, 2011

Grails Taglib : The template composition pattern

Grails leverages Sitemesh, a decorator engine, to support view layouts. Sitemesh layout is restricted to page layout and are not applicable to do template layout. For example a template layout which describes the composition of a table and a toolbar.

The purpose of this taglib is to define a ui composition pattern to leverage template layout.

The tags

The taglib will expose the following tags :
  • ui:composition: The main tag. It takes a template attribute and it has ui:define tags as childrens.
  • ui:define: The children tags. It takes a name attribute and
    a composition attribute which is a trick to create the parent<->children relationship. The body content of each ui:define tags will be used inside the ui:composition template.

The taglib

Writing taglib in Grails is a breeze on the contrary of frameworks like JSF.

Composition contains a simple map:


The view

The "table" template



This taglib can replace Sitemesh layout however Sitemesh provides convention over configuration for layouts and Sitemesh is in Java and therefore much faster than Groovy [1].

UI composition pattern is more generic and leverage template layout and template reusing.

[1] Mean response time for 50 concurrent thread :
benchSitemesh 0.019751053s
benchComposition 0.027618479s


  1. You can annotate defines map in Composition class with @Delegate and you should be able to access it directly from composition:

    out << g.render(template: attrs.template, model: composition)

    attrs.composition.put(, body)


    attrs.composition[] = body

    anyway this could lead to a little delay in executions, I didn't try it