Planning your first Taglib

In my last post I described why I love Grails Taglibs and how they increase productivity, reduce the amount of code in GSPs (therefore reducing maintenance time) and help to keep your code consistent; this post, as promised, is aimed at PHP developers moving over to Grails and will show newbies how to use Taglibs and reduce your coding time and re-used code.

When writing your application, you will quickly come across blocks of code that you will re-use – in fact, a good indicator of this is the over use of CMD-V (or Ctrl-V for those developers still using Windows) – these will be the first methods in your taglib.

We shall use the DVD Library from my last post to illustrate how I would begin and a couple of example methods within the taglib itself. Before we begin, I’ll quickly describe namespaces and what they mean for taglibs.

Many application programmers will already know what namespaces are, and many [OOP] PHP programmers will already use namespaces even if they don’t necessarily use the term; you will often find that many of your classes use the same methods – examples could be save(), delete() and duplicate() etc – could you imagine how many extra bytes you’d have to use if you had to give each method a different name to prevent your app crashing? saveDVD(), saveBook(), saveCD() – which all essentially do the same task seems (and indeed IS) a waste of memory when you can just have save().

So, property and method scope is important to understand, no matter which language, framework or operating system you are developing for and namespaces help us to identify this scope quickly. Another benefit of this, which has actually just popped into my mind, is if you want to slightly modify how one of the built in tags work – you can simply create your own taglib with a method of the same name, change it’s namespace and voila!

To set up your namespace, you simply need to declare it inside the taglib’s class definition, but before any of the methods, e.g:

  1. class DVDLibraryTagLib { static namespace="seb"   }

Your tags will then be accessed using:

  1. <seb:coverImage product="${dvd}" />

Those of you who already use Grails, will most likely recognise the select tag:

  1. <g:select from="${DVD.List()}" />

Which renders a combo box with each instance of our class DVD in the database. If you wished to render this combo box with slightly different html – for example if HTML 5 introduces a new attribute for the <select> or <option> tags – rather than poking around in the, already very good, ModelTagLib you could simply create the new method in your own taglib and call it thus:

  1. <seb:select from="${DVD.List()}" />

The other benefit is that you can keep all your application’s custom taglibs easily recognisable; if you are working in a team (unlike myself) other programmers can easily identify which code has been written by their team and which is standard grails code. The number of times I’ve picked up someone else’s broken PHP code and thought to myself… “My goodness, I’ve never even heard of the function get_all_empty_recordsets” only to discover that it’s actually hidden deeply in a custom function.

As mentioned last time, tag libraries are initialised simply by creating a new file in the /grails-app/taglib/ folder with the naming convention <yournamehere>TagLib.groovy – so it is only really the filename that is down to you to choose. As with everything us programmers do, your filename should be chosen sensibly (as should your namespaces!) – I generally tend to use my application or plugin’s name for taglib filenames and an abbreviation of it for the namespace:

e.g. DVDLibraryTagLib.groovy would contain the namespace <dvdLib:coverImage />

Having a taglib called SebsTagLib.groovy means nothing to anyone else working on your code, how is the junior developer, Jamie, going to know which tags are going to be in the SebsTagLib.groovy file and which will be in the MarcsTagLib.groovy? He knows instantly which to use from DVDLibraryTagLib.groovy and which to use from CDLibraryTagLib.groovy.

For more information on Taglibs see the official grails documentation on the matter, and I shall be continuing my series on evolving from a PHP developer to a Grails convert over the next few months.

Leave a Reply

Your email address will not be published. Required fields are marked *