Quantcast

Grails Dynamic Tag Libraries

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Grails Dynamic Tag Libraries

graemer
Hello all,

Over the weekend my daughter kept me up all night so while she was
watching Monsters Inc I naturally started thinking about Grails ;-)

In particular as discussion I had with Guillaume about being able to
easily create tags for Grails. So I started experimenting and came up
with Grails tag libraries. Essentially within the "grails-app/taglib"
directory there will be a "ApplicationTagLib.groovy" file . This
specially taglib is available to all controllers..

To add a new tag you add a new closure like this:

@Property myTag = { attrs ->
       this.out << "<mytag attrName='${attrs['someAttr']}' />
}

thats for simple tags.. if you want a more complex tag you can do:

@Property bodyTag = { attrs, body ->
     this.out << "<bodytag>"
        body()
     this.out << "</bodytag>"
}

The body is passed in as a closure so that means you can also
implement iterative tags and logical tags:

@Property iterTag = { attrs, body ->
     this.out << "<itertag>"
        3.times {
            body()
        }
     this.out << "</itertag>"
}

in GSP you can then call your tags dynamically:

<gr:myTag someAttr="blah" />
<gr:bodyTag>the body</gr:bodyTag>
<gr:iterTag>do this 3 times</gr:iterTag>

Unfortunatley for JSP you have to either call the tag like this:

<gr:invokeTag name="myTag" someAttr="blah" />

Or modify the TLD, I will be creating a generic class that can be used
within the TLD to add your owner JSP tags that delegate to the Grails
taglib. Another option is that we could generate the TLD from the tag
lib files defined in your project so they both work.

Oh and If you extend it with a name that matches your controller name
(say "UserTagLib" for "UserController" you can add extra tags specific
to that controller). This why the tags replace the view helpers
concept in RoR

Anyway I think its quite cool :-)

Graeme
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Grails Dynamic Tag Libraries

Guillaume Laforge-2
On 16/01/06, Graeme Rocher <[hidden email]> wrote:
> [...]
> Anyway I think its quite cool :-)

Damn, it is damn cool!
Thanks to your daughter ;-)

--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Grails Dynamic Tag Libraries

Dierk König
In reply to this post by graemer
That's a cool concept! And makes writing custom tags as easy as
writing beans.

I don't understand why the tag closure itself produces custom markup
again like "<mytag>". Shouldn't it produce pure content or
html markup?

cheers
Mittie



> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]On Behalf
> Of Graeme Rocher
> Sent: Montag, 16. Januar 2006 12:19
> To: [hidden email]
> Subject: [grails-dev] Grails Dynamic Tag Libraries
>
>
> Hello all,
>
> Over the weekend my daughter kept me up all night so while she was
> watching Monsters Inc I naturally started thinking about Grails ;-)
>
> In particular as discussion I had with Guillaume about being able to
> easily create tags for Grails. So I started experimenting and came up
> with Grails tag libraries. Essentially within the "grails-app/taglib"
> directory there will be a "ApplicationTagLib.groovy" file . This
> specially taglib is available to all controllers..
>
> To add a new tag you add a new closure like this:
>
> @Property myTag = { attrs ->
>        this.out << "<mytag attrName='${attrs['someAttr']}' />
> }
>
> thats for simple tags.. if you want a more complex tag you can do:
>
> @Property bodyTag = { attrs, body ->
>      this.out << "<bodytag>"
>         body()
>      this.out << "</bodytag>"
> }
>
> The body is passed in as a closure so that means you can also
> implement iterative tags and logical tags:
>
> @Property iterTag = { attrs, body ->
>      this.out << "<itertag>"
>         3.times {
>             body()
>         }
>      this.out << "</itertag>"
> }
>
> in GSP you can then call your tags dynamically:
>
> <gr:myTag someAttr="blah" />
> <gr:bodyTag>the body</gr:bodyTag>
> <gr:iterTag>do this 3 times</gr:iterTag>
>
> Unfortunatley for JSP you have to either call the tag like this:
>
> <gr:invokeTag name="myTag" someAttr="blah" />
>
> Or modify the TLD, I will be creating a generic class that can be used
> within the TLD to add your owner JSP tags that delegate to the Grails
> taglib. Another option is that we could generate the TLD from the tag
> lib files defined in your project so they both work.
>
> Oh and If you extend it with a name that matches your controller name
> (say "UserTagLib" for "UserController" you can add extra tags specific
> to that controller). This why the tags replace the view helpers
> concept in RoR
>
> Anyway I think its quite cool :-)
>
> Graeme
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Grails Dynamic Tag Libraries

graemer
On 16/01/06, Dierk Koenig <[hidden email]> wrote:
> That's a cool concept! And makes writing custom tags as easy as
> writing beans.
>
> I don't understand why the tag closure itself produces custom markup
> again like "<mytag>". Shouldn't it produce pure content or
> html markup?

Yes it was just an example :)

>
> cheers
> Mittie
>
>
>
> > -----Original Message-----
> > From: [hidden email] [mailto:[hidden email]]On Behalf
> > Of Graeme Rocher
> > Sent: Montag, 16. Januar 2006 12:19
> > To: [hidden email]
> > Subject: [grails-dev] Grails Dynamic Tag Libraries
> >
> >
> > Hello all,
> >
> > Over the weekend my daughter kept me up all night so while she was
> > watching Monsters Inc I naturally started thinking about Grails ;-)
> >
> > In particular as discussion I had with Guillaume about being able to
> > easily create tags for Grails. So I started experimenting and came up
> > with Grails tag libraries. Essentially within the "grails-app/taglib"
> > directory there will be a "ApplicationTagLib.groovy" file . This
> > specially taglib is available to all controllers..
> >
> > To add a new tag you add a new closure like this:
> >
> > @Property myTag = { attrs ->
> >        this.out << "<mytag attrName='${attrs['someAttr']}' />
> > }
> >
> > thats for simple tags.. if you want a more complex tag you can do:
> >
> > @Property bodyTag = { attrs, body ->
> >      this.out << "<bodytag>"
> >         body()
> >      this.out << "</bodytag>"
> > }
> >
> > The body is passed in as a closure so that means you can also
> > implement iterative tags and logical tags:
> >
> > @Property iterTag = { attrs, body ->
> >      this.out << "<itertag>"
> >         3.times {
> >             body()
> >         }
> >      this.out << "</itertag>"
> > }
> >
> > in GSP you can then call your tags dynamically:
> >
> > <gr:myTag someAttr="blah" />
> > <gr:bodyTag>the body</gr:bodyTag>
> > <gr:iterTag>do this 3 times</gr:iterTag>
> >
> > Unfortunatley for JSP you have to either call the tag like this:
> >
> > <gr:invokeTag name="myTag" someAttr="blah" />
> >
> > Or modify the TLD, I will be creating a generic class that can be used
> > within the TLD to add your owner JSP tags that delegate to the Grails
> > taglib. Another option is that we could generate the TLD from the tag
> > lib files defined in your project so they both work.
> >
> > Oh and If you extend it with a name that matches your controller name
> > (say "UserTagLib" for "UserController" you can add extra tags specific
> > to that controller). This why the tags replace the view helpers
> > concept in RoR
> >
> > Anyway I think its quite cool :-)
> >
> > Graeme
>
Loading...