Useful Velocity Methods
isSet() - Field is set
${esc.h}if(${esc.d}UtilMethods.isSet(${esc.d}content.variable))
.contains() - Field contains value: (used with check boxes, radio boxes and drop downs. These add a comma to the content so you can't use .equals() )
${esc.h}if(${esc.d}content.officeOrDepartment.contains('Value'))
.equals() With string values:
${esc.h}if(${esc.d}term.equals('Fall'))
With variables:
${esc.h}if(${esc.e}${esc.d}dateMonth.equals(${esc.d}currentMonth))
Request Parameter- This will get the value of id, if it is passed in via url or a post
${esc.d}request.getParameter("id")
Count in a loop:
${esc.d}velocityCount
This variable is updated automatically in the for-loops, it starts with value: 1 Check if equals to value:
${esc.h}if(${esc.d}velocityCount == 1)
Date Format: Month:
${esc.d}date.format('MMMM', ${esc.d}content.date))
Year:
${esc.d}date.format('yyyy', ${esc.d}content.date))
MM/DD/YYY:
${esc.d}date.format('MM/dd/yyyy', ${esc.d}content.endSeason) ${esc.h}set(${esc.d}dateYear = ${esc.d}date.get('yyyy')) ${esc.h}set(${esc.d}nextYear = ${esc.d}webapi.parseInt(${esc.d}dateYear) + 1) //returns current year+1
Another formatting for date:
${esc.d}UtilMethods.dateToHTMLDate(${esc.d}content.gameDatetime.toString(),'EEE MMM dd hh:mm:ss z yyyy','M/d/yyyy') Eg: 7/6/2006
${esc.d}UtilMethods.dateToHTMLDate(${esc.d}content.gameDatetime.toString(),'EEE MMM dd hh:mm:ss z yyyy','hh:mm:ss a') Eg: 11:30 AM
Empty Container
${esc.h}if(${esc.d}CONTAINER_NUM_CONTENTLETS == 0)
List Size() -Can be used to determine if a pullContent query brought any results..
${esc.h}pullContent.... ${esc.h}if(${esc.d}list.size() > 0)
.findFolder() - Returns Folder where link was created:
${esc.h}if(${esc.d}webapi.findFolder(${esc.d}content.form2LinkURL)=='/about_us/departments_and_offices/forms/admissions/international/')
with a variable:
${esc.h}set(${esc.d}folder='international') ${esc.h}if(${esc.d}webapi.findFolder(${esc.d}content.form1LinkURL)==${esc.d}render.eval('/admissions/admissions_forms/${esc.d}folder/'))
Random Generator: (usually we have a query first that pulls so many items , eg: 5, then we use the generator to randomly pick one )
${esc.h}set(${esc.d}list = ${esc.d}utilMethods.randomList(${esc.d}list,1)) ${esc.h}set(${esc.d}content = ${esc.d}list.get(${esc.d}math.toInteger(0)))
If selecting only one random item, then we don't need to loop through the list- we can use ${esc.d}
content.variable
Replace String
${esc.d}string2=${esc.d}string1.replaceAll("/", "//") - replaces all / with //
String: Substring and Index of..
${esc.d}VTLSERVLET_URI.substring(0, ${esc.d}VTLSERVLET_URI.indexOf('/',1))
${esc.d}VTLSERVLET_URI.indexOf('/',1)) we start looking for / from the position 1 not including the character in 0
Phone Formatter:
${esc.d}stringsapi.formatPhoneNumber('12:34-56789001')