Showing posts with label tags. Show all posts
Showing posts with label tags. Show all posts

Monday, August 27, 2012

The not so obvious fact about tag libraries

I've came across this thing today and quite frankly I feel a little bit ashamed that I didn't know it yet. The concept of tag libraries is not new in the JSP world. It's just unusual to write tags because one needs to provide a tag library description file along with it and that increases the complexity of the solution (or so it is seen).

The nice thing about tag libraries is that they don't necessarily need a TLD file to drive them - a folder is more than enough for simple cases:

File /WEB-INF/tags/hello.tag
Hello, world! from a tag
File /index.jsp
<@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

<tags:hello />
It's really that simple!

There's obviously more to it (how to pass the tag body, how to pass parameters to the tag) - maybe next time :)

Monday, November 29, 2010

Grails, plugins and tags with templates

Hi all,

If you've wondered if it's possible to render a view from a tag I rush to say it is absolutely possible. You just use the render method and specify the template to be rendered like in this example:
class ExampleTagLib {
def example = { attr, body ->
out << render(template: '/tags/example')
}
}

Ok, that was easy. However things get a little bit more interesting when we try to do the same thing from within a plugin. The problem here is that the render method tries to find the view to be rendered in the application and not in the plugin.
The remedy to this problem is simple: you need to append the plugin name as a parameter to the render method like this:
class ExampleTagLib {
def example = { attr, body ->
out << render(template: '/tags/example', plugin: 'example-plugin')
}
}

Here you can find a ready-to-run example.

Have fun!