May 14 2008

Okay, Grails, That Was Cute: Taglibs

Published by Robert Fischer at 1:19 pm under To Be Categorized

So, a while back I was impressed by Rails routing. It was a nice little surprise which came out of left field to impress me: I love it when frameworks do that.

Grails just had its time — taglibs. Now, because of the pain of JSP tag libraries, I’ve avoided anything that sounded anything like UI taglibs. But, I was starting to repeat myself in views, and in such a way that partials seemed a bit hokey. This is where custom tags come in.

So, I sucked it up and checked out Grails taglibs. The limited documentation on the page scared the crap out of me, because I assumed I was going to get stuck and there’d be no help.

Turned out I didn’t need any. Nice.

The code that repeated looked like this:


            <g:if test="${data.otherDobs}">
                <g:link action="otherDobs" id="${id}">
                    Other Date(s) of Birth
                </g:link>
            </g:if>
            <g:else>&nbsp;</g:else>

The argument to “test”, the “action”, the “id”, and the text to display all varied.

After the taglib, it looks like this:


            <g:moreData test="${data.otherDobs}"
                    action="otherDobs" id="${id}">
                    Other Date(s) of Birth
            </g:moreData>

The taglib was just:


class MyAppTagLib {
  def moreData = { attr, body ->
    if(attr['test']) {
      out << link([action: attr['action'], id: attr['id']], { body() })
    } else {
      out << ' '
    }
  }
}

Cute. For bonus points, I could have passed all the attrs except for ‘test’ down into link, but I decided against doing that right now. All the magic was freaking me out.

Popularity: 1% [?]

4 Responses to “Okay, Grails, That Was Cute: Taglibs”

  1. Mikeon 14 May 2008 at 2:25 pm

    Another cool trick I recently discovered was setting a namespace in the tag lib:

    static namespace = “data”

    let’s you prefix your taglib with ‘data:’ rather than ‘g:’

    <data:moreData …

    Once you have a bunch of tag lib classes, it makes it a little easier to know which *TagLib.groovy class to look in to find the implementation.

  2. Robert Fischeron 14 May 2008 at 3:32 pm

    Not a bad plan, Mike.

    Missed you at GUM, BTW.

  3. ryanon 15 May 2008 at 12:28 pm

    Those taglibs are one of the reasons I’m so excited about grails, (also one of the reason my co-workers are ready to throw me out since I got so giddy about it, an they’re still using traditional Java on their project.)

    I created one, (clearly for us in the states) for a state drop down list that also allows a passed in default choice for state if one had been selected previously and is in session or otherwise.

    Our project doesn’t have a States table to pull from and it was getting to be a pain typing it again and again. We then tried including the list but it was difficult to pull off if someone was using different names for the already selected state, etc. A custom tag was the answer! Went from all those lines of code down to one line in the GSP.

  4. karthikon 15 May 2008 at 5:52 pm

    I hated tag libs too and i stayed away from JSP / java web development tier in general for a few years - i just found the tag lib reuse (& programming) model irritating

    and then i found Apache Wicket!

    So what you do is write a component that extends from the framework supplied Link class -

    class DobLink extends Link{

    DobLink(String id,List dobs){
    super(id, new Model(dobs));
    }

    @Override
    public boolean isVisible(){
    List dobs = model.getModelObject();
    return !dobs.isEmpty();
    }

    @Override
    public void onClick(){
    setResonsePage(new DobPage());
    }

    }

    In the HTML template - (wicket works with plain HTML templates)

    Other Date(s) of Birth

    and on the server-side page class -

    add(new DobLink(”dob”, dobList ));

    Now the link visibility will toggle based on the dobList size.
    I would strongly recommend Wicket for all tag lib haters.

    There is a grails-wicket plugin apparently. Need to take a look!

Trackback URI | Comments RSS

Leave a Reply

Green Web Hosting! This site hosted by DreamHost.