More Fun with Velocimacros

Posted on May 23, 2011
Filed Under Programming | 17 Comments | Share this post via splatforms.com

One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions known as Velocimacros. The Splatforms Task specification provides a section in the Task definition where Task Developers can include any number of Velocimacros needed during the Task processing. To get an idea of the kinds of things that you can do with a Velocimacro, check out the Velocimacros section of the Velocity User’s Guide on the Apache Velocity web site.

Most of the web sites that serve as a destination target for the data found in a Link *form accept “tags” as one of their input fields, and most of those accept the tags input as a single, comma-delimited string. A single comma-delimited string is the expected input from a Link *form, so passing on the tags to most target destinations is simply matter of sending the user’s input as is. For those sites that has a field length limitation, a simple #crop Velocimacro will handle that requirement. But there are a few sites, such as Delicious and Trunk.ly, that expect their tag string input to be delimited by spaces, not commas. For that, we need little more sophisticated processing, but something that a Velocimacro can easily handle.

#macro(fixTags $tagString)
  #if ($tagString)
    #set ($separator = "")
    #set ($tagArray = $tagString.split(","))
    #foreach ($tag in $tagArray)
      #set ($tag = $tag.trim())
      #set ($tag = $tag.replace(" ","_"))
$separator$tag
      #set($separator = " ")
    #end
  #end
#end

The code fairly self-explanatory, but let’s break it down, just for fun:

#macro(fixTags $tagString)

This is the macro definition; the first argument inside of the parenthesis is the name of the macro, which is used to invoke it, and the second is the single argument that will be passed to the macro at execution time. To invoke the #fixTags macro, you would type:

#fixTags($tags)

The next line just verifies that there are, in fact, tags to be processed:

  #if ($tagString)

While this is not technically required, there is no point in going through the rest of the process if the tag string is empty, so this screens out all of the calls made where there are no tags.

The next couple of lines set up an empty string for the first tag separator, and split the comma-separated string in an array of strings:

    #set ($separator = "")
    #set ($tagArray = $tagString.split(","))

Once we have an array, we can use a for loop to process each item in the array individually:

    #foreach ($tag in $tagArray)
      #set ($tag = $tag.trim())
      #set ($tag = $tag.replace(" ","_"))
$separator$tag
      #set($separator = " ")
    #end

Inside the loop, for every tag, we trim off any excess white space, convert any spaces to underscores, and then output the separator and the cleaned-up tag. Once that’s done, we convert the separator to a single space for the next time through (the first time only, the separator is an empty string, as we do not want to start out with a space).

That’s about all there is to it. If you want to see another example of how Velocimacros can be used in a Splatform Task, check out the Spintax Velocimacro. And if you really want to have some fun, sign up as Task Developer and try it out for yourself!

Comments

17 Responses to “More Fun with Velocimacros”

  1. pligg.com on May 23rd, 2011 12:31 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions known as Velocimacros. The Splatforms Task specificat…

  2. pligg.com on May 23rd, 2011 12:32 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velo…

  3. pligg.com on May 23rd, 2011 12:33 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions k…

  4. pressitfor.me on May 23rd, 2011 12:33 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velo…

  5. diggdirect.com on May 23rd, 2011 12:44 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions k…

  6. bloglink.org on May 23rd, 2011 12:45 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velo…

  7. whatsyourmessage.net on May 23rd, 2011 12:45 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velo…

  8. digg.ohozaa.com on May 23rd, 2011 12:54 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions known as Velocimacros. The Splatforms Task specificat…

  9. socialbookmarking.co.in on May 23rd, 2011 12:55 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions known as Velocimacros. The Splatforms Task specificat…

  10. Sponter.com on May 23rd, 2011 12:58 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions known as Velocimacros. The Splatforms Task specificat…

  11. merolinks.com on May 23rd, 2011 1:07 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions known as Velocimacros. The Splatforms Task specificat…

  12. telesmatic.com on May 23rd, 2011 1:08 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velo…

  13. www.sporticts.com on May 23rd, 2011 1:16 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions known as Velocimacros. The Splatforms Task specificat…

  14. xehh.com on May 23rd, 2011 1:18 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions known as Velocimacros. The Splatforms Task specificat…

  15. Yemle.com on May 23rd, 2011 1:18 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions k…

  16. yodesi.com on May 23rd, 2011 1:19 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions k…

  17. indiasurfer.com on May 23rd, 2011 1:29 pm

    More Fun with Velocimacros…

    One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions known as Velocimacros. The Splatforms Task specificat…