Here's a first.  I don't think I've ever put a piece of code I've written into my blog.  This little gem below is for a side-project of mine.  It's a Grails custom tag.  You call it on a GSP, a groovy server page, similar to the way you call an HTML tag.

  def formattedPhone = {attrs, body ->
    def rawNumber = attrs.phoneNumber
    out << "("+rawNumber.take(3)+") "+rawNumber.drop(3).take(3)+"-"+rawNumber.drop(6)
}

That looks kind of like this:

<g:formattedPhone phoneNumber="${user.mobileNumber}"/>

(On an amusing side note, when you make a string with a ${} set in it on a GSP, the code inside the handlebars is treated like an expression, and evaluated prior to the page load.  That's not the funny part.  The funny part is that those are called gStrings.  Aren't programmers a riot?)

Why is this my gratitude today?  Several reasons.  

First, I've been working with Grails for several years now, and since I'm working on a side project of my own, I get to build it the way I want, not the way that's been dictated to me.  I've known about custom tags for a while, but never had the chance to really use them before.

Second, I taught myself how to do this.  I had a vague idea of how tags worked in general, and why they were a good idea.  So I hopped on the net, did a little googling, and after reading a few interesting tidbits, banged out a bunch of tags.

A friend of mine taught me the word for this sort of behavioral ability before - it's called auto didacticism.  It's being a student, and one's own teacher too.  I'm glad to have it.  It serves me well.

Posted
AuthorMako Allen
Categories365 Gratitude