Splatforms and Velocimacros

Posted on August 9, 2010
Filed Under Splatforms | 1 Comment | Share this post via splatforms.com

Although we have already paid tribute to the value of Apache Velocity, one thing that we neglected to mention was one of its most powerful features, Velocity macros, or Velocimacros as they are officially known. According to the Velocity User Guide,

The #macro script element allows template designers to define a repeated segment of a VTL template. Velocimacros are very useful in a wide range of scenarios both simple and complex.

At Splatforms.com, Velocimacros are used in Task development, and can be included in the Task definition, or in any field that is resolved using Velocity. Placing Velocimacros in the Task definition makes those Velocimacros available to every Velocity operation related to the Task, while placing them in individual fields limits their availability to just that specific field.

One of the simplest Velocimacros currently in use is called twodigits, and as its name implies, its sole purpose is to ensure that a numeric field contains two digits, including a leading zero, if necessary. While there are a number of potential uses for such a function, the primary use of this Velocimacro is to create two-digit month, day, hour, and seconds fields from the integers returned by various methods of the JodaTime DateTime objects used in Events *forms. The logic is pretty simple: if the value of the integer passed is less than 10, then an extra zero is included along with the original number passed; otherwise, just the number passed is returned.

#macro(twodigits $number)
#if($number<10)0#end$number#end

For something a little more sophisticated, you can take a look at the fixtags Velocimacro used in the Task that posts Link *forms to Multiply. Here we have a Velocimacro that calls another Velocimacro (crop) to ensure that no tags passed are longer than the Multiply tag length limit of 20:

#macro(fixtags $tags)
#foreach($tag in $tags.split(","))
#crop($tag 20),#end#end

#macro(crop $string $max)
#if($string.length()>$max)$string.substring(0,$max)#else$string#end#end

Like Velocity itself, Velocimacros give Task Developers the power to create Tasks in a way that will maximize the potential for success. Anything that a developer can do to make sure that the data entered or loaded into a *form is acceptable to the target site’s validation processes helps ensure that the Task will be successful. Velocimacros are just one more tool available to help make that happen.

Comments

One Response to “Splatforms and Velocimacros”

  1. Splatforms, Velocimacros, and Spintax : Splatforms on February 2nd, 2011 6:49 am

    […] have already acknowledged the contribution that Velocity has brought to the table related to Task development, but we keep […]