This page last changed on Mar 21, 2006 by [email protected].

Dynamic Tag Libraries

Grails has a wide range of of custom tags built in for both JSP and GSP (see the Tag Library Reference here), however Grails also allows the creation of simple, logical, and iterative custom tags through its simple dynamic tag library mechanism.

The benefit of grails' tags is that they require no additional configuration, no updating of TLD descriptors, and can be auto-reloaded at runtime without a server restart. This makes developing tags feel as if you were just developing another part of the view and increases their usefulness tenfold.

Warning

Grails Tag libraries require a little extra work to integrate into JSP and are more seamlessly integrated into GSP because of its dynamic nature. See the last section for how to use grails tags from JSP

Simple tags

To create new tags open the "grails-app/taglib/ApplicationTagLib.groovy" file or create a new class ending in "TagLib". To create a simple tag add a new closure property that takes 1 argument which are the attributes of the tag:

@Property includeJs = { attrs ->
    out << "<script xsrc='scripts/${attrs['script']}.js' />"
}

To call your tag your from a GSP page use the "g" prefix followed by the tag property name:

<g:includeJs script="myscript" />

Logical tags

You can also create logical tags by using a closure syntax that takes 2 arguments, the attributes of the tag and the body of the tag as a closure:

@Property isAdmin = { attrs, body ->
     def user = attrs['user']
     if(user != null && checkUserPrivs(user)) {
           body()
     }
}

The tag above checks if the user is an administrator and invokes the body of the tag if he/she is:

<g:isAdmin user="${myUser}">
    // some restricted content
</g:isAdmin>

Iterative tags

And of course you can create iterative tags:

@Property repeat = { attrs, body ->
    def i = Integer.valueOf( attrs["times"] )
    def current = 0
    i.times {
           body( ++current ) // pass the current iteration as the groovy default arg "it"
    }
}

To call your iterative tag do the following:

<g:repeat times="3">
<p>Repeat this 3 times! Current repeat = ${it}</p>
</g:repeat>

Using Grails tag libs from JSP

To use a Grails taglib definition in JSP you can use the JSP "invokeTag" tag which will call a tag defined in the Grail tag library:

<g:invokeTag name="includeJs" script="myscript" />
<g:invokeTag name="isAdmin" user="${myUser}">
    // some restricted content
</g:invokeTag >
<g:invokeTag name="repeat" times="3">
<p>Repeat this 3 times! Current repeat = <c:out value="${it}" /></p>
</g:invokeTag>

If you want your tags to appear like normal JSP tags, here's what you need to do:

1) Create a new java class that sub classes the Grails org.codehaus.groovy.grails.web.taglib.jsp.JspInvokeGrailsTagLibTag class and calls the "setName()" method in the constructore of your new class passing the name of your tag:

package com.mycompany.taglib;
public class IncludeJsTag extends JspInvokeGrailsTagLibTag {
   public static final String TAG_NAME = "includeJs";
   public IncludeJsTag() {
       super.setName(TAG_NAME);
   }
}

2) JSP requires declarative tag library definition files (TLD) for each tag, to do this modify the "web-app/WEB-INF/tld/grails.tld" file and add the necessary entries that point to your class:

<tag>
        <name>includeJs</name>
        <tag-class>com.mycompany.taglib.IncludeJsTag</tag-class>
        <body-content>JSP</body-content>
        <variable>
            <name-given>it</name-given>
            <variable-class>java.lang.Object</variable-class>
            <declare>true</declare>
            <scope>AT_BEGIN</scope>
        </variable>
        <dynamic-attributes>true</dynamic-attributes>
    </tag>

3) You can then call your tag from JSP like a normal JSP tag:

<g:includeJs script="myscript" />
Document generated by Confluence on Mar 29, 2006 08:46