A collection of Velocimacros of a non-web nature that may be useful in *any* Velocity application. These may make use of the VelocityTools' Generic tools.


#**
 * For displaying an alternate value when the primary one is "null".
 * 
 * @param the primary variable
 * @param the alternate value or variable
 * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
 *#
#macro( altnull $variable $alternate )
#if( "$!variable" == "" )$!alternate#else$variable#end##
#end


#**
 * For truncating a variable's output at a specific length.
 *
 * Usage:
 *   "#truncate( 'this is too long' 10 )" -> "this is to..."
 * 
 * @param the variable to be truncated
 * @param the length of the output (not including the ellipses!)
 * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
 *#
#macro( truncate $stringVariable $length )
#set( $truncateMe = "$!stringVariable" )
#if( $truncateMe.length() <= $length)$truncateMe#else$!{truncateMe.substring(0,$length)}...#end##
#end

#**
 * Convenience directive to invoke a method and ignore the return value.
 *
 * Usage:
 *    #call( $hashtable.put("foo", "bar") )
 *
 *#
#macro( call $foo )#if($foo)#**##end#end


#*
 * Convenience macro to return "unknown" if null value
 * @author Ted Husted
*# 
#macro (unknown $name $code)
    #if($code )
        $!name [$code]
    #else
        <i>Unknown</i>
    #end
#end
#*
 * Convenience macro to return "Not Specified" if null value
 * @author Ted Husted
*#
#macro (notSpecified $value)
    #if($value )
        $value
    #else
        <i>Not Specified</i>
    #end
#end
#*
 * Convenience macro to return "Pending" if null value
 * @author Ted Husted
*#
#macro (pending $value)
    #if($value )
        $value
    #else
        <i>Pending</i>
    #end
#end
  • No labels