Useful Velocity Methods

Last Updated: Feb 1, 2022
documentation for the dotCMS Content Management System
isSet() Field is set
  #if($UtilMethods.isSet($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() )
  #if($content.officeOrDepartment.contains('Value'))
  
.equals() With string values:
  #if($term.equals('Fall'))
  
With variables:
  #if(!$dateMonth.equals($currentMonth))
  
Request Parameter- This will get the value of id, if it is passed in via url or a post
  $request.getParameter("id")
  
Count in a loop:
  $velocityCount
  
This variable is updated automatically in the for-loops, it starts with value: 1 Check if equals to value:
  #if($velocityCount == 1)
  
Date Format: 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
  
Empty Container
 
  #if($CONTAINER_NUM_CONTENTLETS == 0)
  
List Size() -Can be used to determine if a pullContent query brought any results..
  #pullContent....
  #if($list.size() > 0)
  
.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: (usually we have a query first that pulls so many items , eg: 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 use $content.variable Replace String
  $string2=$string1.replaceAll("/", "//")  - replaces all / with //
    
String: Substring and Index of..
  $VTLSERVLET_URI.substring(0, $VTLSERVLET_URI.indexOf('/',1))
  $VTLSERVLET_URI.indexOf('/',1))  we start looking for / from the position 1 not including the character in 0
    
Phone Formatter:
  $stringsapi.formatPhoneNumber('12:34-56789001')
  

On this page

×

We Dig Feedback

Selected excerpt:

×