(Quick Reference)
Tag Library Usage
A tag library fulfills role of "view helper" in the Model View Controller (MVC) pattern and helps with GSP rendering. In Grails a tag library is a class with a name that ends in the convention "TagLib" and lives in the
grails-app/taglib directory. Use the
create-taglib command to create a tag library:
grails create-taglib format
or with your favourite IDE or text editor make a new file with the name
FormatTagLib in
grails-app/taglib as given below
import java.text.SimpleDateFormatclass FormatTagLib {
def dateFormat = { attrs, body ->
out << new SimpleDateFormat(attrs.format).format(attrs.value)
}
}Each Closure property in a tag library that takes one or two arguments is considered a tag. The first argument (typically named
attrs) will contain the attributes of the tag whilst the optional second argument (typically named
body) is Closure that represents the inner HTML of the tag declaration from the GSP.
Refer to the user guide topic on
Tag Libraries for more information.