Useful Velocity Methods
Values#
isSet() — is the field set?
#if($UtilMethods.isSet($content.variable))
.contains() checks if the field contains a value. Used with check boxes, radio boxes and drop downs; these add a comma to the content, so you can't use .equals()
#if($content.officeOrDepartment.contains('Value'))
.equals() checks string values:
#if($term.equals('Fall'))
For variables:
#if(!$dateMonth.equals($currentMonth))
Request Object#
Request parameter: This will get the value of id, if it is passed in via URL or a POST. For more, see Request, Response, Session.
$request.getParameter("id")
Looping#
Count within a loop:
$velocityCount
This variable is updated automatically in the for-loops, it starts with value: 1. To check if equal to a value:
#if($velocityCount == 1)
Date Formatting#
Month:
$date.format('MMMM', $content.date))
Year:
$date.format('yyyy', $content.date))
MM/DD/YYY:
$date.format('MM/dd/yyyy', $content.endSeason) #set($dateYear = $date.get('yyyy')) #set($nextYear = $webapi.parseInt($dateYear) + 1) ## returns current year+1
Another formatting for date:
$UtilMethods.dateToHTMLDate($content.gameDatetime.toString(),'EEE MMM dd hh:mm:ss z yyyy','M/d/yyyy') ## Eg: 7/6/2006 $UtilMethods.dateToHTMLDate($content.gameDatetime.toString(),'EEE MMM dd hh:mm:ss z yyyy','hh:mm:ss a') ## Eg: 11:30 AM
Containers#
Empty container check
#if($CONTAINER_NUM_CONTENTLETS == 0)
Lists#
.size() Can be used to determine if a content query brought any results..
#if($list.size() > 0)
Folders#
.findFolder() - Returns Folder where link was created:
#if($webapi.findFolder($content.form2LinkURL)=='/about_us/departments_and_offices/forms/admissions/international/')
With a variable:
#set($folder='international') #if($webapi.findFolder($content.form1LinkURL)==$render.eval('/admissions/admissions_forms/$folder/'))
Random Generator#
Start with a query that pulls so many items — e.g., 5 — then we use the generator to randomly pick one:
#set($list = $utilMethods.randomList($list,1)) #set($content = $list.get($math.toInteger(0)))
If selecting only one random item, then we don't need to loop through the list; we can go straight to $content.{variable}
Strings#
Replace string:
$string2=$string1.replaceAll("/", "//") ## replaces all / with //
Substring and string index:
$VTLSERVLET_URI.substring(0, $VTLSERVLET_URI.indexOf('/',1)) $VTLSERVLET_URI.indexOf('/',1))
The above example starts looking for / from the position 1, not including the character in 0.
Phone Formatter:
$stringsapi.formatPhoneNumber('12:34-56789001')