Useful Methods

Velocity Count:

This useful little variable acts as a counter in a foreach loop i.e. no need for #set($i = $i + 1)

  1. $velocityCount  

List Size() - Can be used to determine if a pullContent query brought any results:

  1. #pullContent....  
  2. #if($list.size() > 0)  

Find Folder

.findFolder() - Returns Folder where link was created:

  1. #if($webapi.findFolder($content.form2LinkURL)=='/about_us/departments_and_offices/forms/admissions/international/')  

you could also do this with a variable:

  1. #set($folder='international')  
  2. #if($webapi.findFolder($content.form1LinkURL)==$render.eval('/admissions/admissions_forms/$folder/'))  

Get Content from inode

GetContent() - used to pull content when given an Inode

  1. #getContentMapDetail($teamInode)  
  2. (When using getContentMapDetail, use $content.variable )  
  3.   
  4. #getContentDetail($teamInode)  
  5. (When using getContentDetail use the $variable without $content.variable)  

String: Substring and Index of:

  1. $VTLSERVLET_URI.substring(0, $VTLSERVLET_URI.indexOf('/',1))  
  2. $VTLSERVLET_URI.indexOf('/',1))  we start looking for / from the position 1 not including the character in 0  

dotCMS Variables:

Here are a selection of variables that can be used at any point on a velocity page.

  1. $quote - "  
  2. $HTMLPAGE_INODE - a5b6dfe0-e4ea-42ed-bdab-eb59677c72c7  
  3. $HTMLPAGE_TITLE - Cookie Example  
  4. $HTMLPAGE_FRIENDLY_NAME - Cookie Example  
  5. $TEMPLATE_INODE - fa94c760-eaf4-4daf-8e64-b06d0a8642b0  
  6. $HTMLPAGE_META - Cookie page  
  7. $HTMLPAGE_SECURE - false  
  8. $VTLSERVLET_URI - /test/cookie-example.html  
  9. $HTMLPAGE_REDIRECT  
  10. $pageTitle  
  11. $pageChannel  
  12. $friendlyName  
  13. $HTML_PAGE_LAST_MOD_DATE  
  14. $EDIT_MODE  

Phone Formatter:

  1. $stringsapi.formatPhoneNumber('12:34-56789001')  

Page Title & Crumbtrail:

  1. #if($request.getParameter("pageTitle"))  
  2. <h1>$request.getParameter("pageTitle")</h1>  
  3. #else  
  4. <h1>$!{pageTitle}</h1>  
  5. #end  
  6.   
  7. <div id="crumbtrail">  
  8. #set($crumbSeparatorImage='/global/images/template/bullet_crumbtrail_arrow.gif')  
  9. #crumbTrail()  
  10. </div>  

Get Category:

  1. #set($inode = $request.getParameter('inode'))  
  2. #getContentDetail($inode)  
  3. #if($ContentletCategories.size() > 0)  
  4. #set($catInode = $ContentletCategories.get(0))  

  1. #if($UtilMethods.isSet($content.form1LinkURL))  
  2.     #if($webapi.findFolder($content.form1LinkURL)==$render.eval('/admissions/admissions_forms/$folder/'))  
  3.           <a href="$content.form5LinkProtocol$content.form1LinkURL">${content.form1LinkTitle}</a><br/>  
  4.     #end  
  5. #end  

Framed image

  1. #set ($frameHeight = 154)  
  2. #set ($frameWidth = 120)  
  3. #set($imageRatio = $math.div($ImageImageHeight, $ImageImageWidth))  
  4. #set($frameRatio = $math.div($frameHeight, $frameWidth))  
  5. <img src="/global/images/mac_mag/homeStories_img.gif" style="background: url(/thumbnail?inode=${ImageImageInode}#if($imageRatio > $frameRatio)&w=$frameWidth#else&h=$frameHeight#end) 50% 50% no-repeat;" height="$frameHeight" width="$frameWidth" alt="image alt text" />  

Parse a container

Parsing a container is not something you need to do very often, they are usually only used in the template

  1. #parseContainer('inode#')  
  2.   
  3. ## Container: TrustLaw - Left Column Container  
  4. ## This is autogenerated code that cannot be changed  
  5. #parseContainer('d5bd353e-39ad-4a11-85bf-c5a62f3bf7fc')  

Sort with velocity

The following code will sort a list based on the title. You can see this in action on detail-article.vtl for the flickr slideshow.

  1. #set ($sortedList = $sorter.sort($myList,'title'))  

If in edit mode

  1. #if($EDIT_MODE)  
  2.     in edit mode, show this  
  3. #end  

Truncate

  1. $!UtilMethods.truncatify($!Body,150)  
  2. ##IF $FIELD IS SET  
  3. #if($UtilMethods.isSet(${Body}))  
  4.     print this  
  5. #end  

STOP script - debugging issues

The #stop script element allows the template designer to stop the execution of the template engine and return. This is useful for debugging purposes.

  1. #stop  

  1. ##link to detail page:  
  2. <a href="page.dot?inode=$!ContentInode">Learn More</a>  
  3. ##in container of detail page use (only if it has 0 contentlet):  
  4.   
  5. #set($inode = $request.getParameter("inode"))  
  6. #getContentDetail("$inode")  
  7.   
  8. ##in contentlet of detail page use:  
  9.   
  10. #set($inode = $request.getParameter('inode'))  
  11. #getContentDetail('$inode')  

Fake Crumb Title

link to dynamic page with fake crumbtitle:

  1. <a href="page.dot?crumbTitle=$UtilMethods.encodeURL("Men's Football")">link</a>  
  2.   
  3. ##change pagetitle to fake crumbtrail:  
  4. #set($pageTitle = $request.getParameter("crumbTitle"))  
  5. #if($request.getParameter("pageTitle"))  
  6.     #set($pageTitle = $request.getParameter("pageTitle"))  
  7. #end  
  8. <h1>#if($crumbTitle)$crumbTitle #else $!{pageTitle} #end</h1>  
  9.   
  10. ## Crumbtrail uses either crumbTitle (if crumbTitle is passed in URL), contentletTitle(if inode is passed in URL) or pageTitle  

Thumbnail Generator:

  1. ##$Picture is whatever variable is used for the uploaded image file.  
  2. #if($UtilMethods.isSet($Picture))  
  3.     <img src="/thumbnail?inode=$!Picture&w=182" />  
  4. #end  
  5.   
  6. ## *h=100 can be used to limit by height instead of width if both height and width are used, a background color should be specified (r=225&b=255&g=255) or else you will get a white background for the spacing around it.  

Get a list of files from repository

  1. ##Get File List  
  2. #fileRepository('/folder/')  
  3. #foreach($file in $filesList)  
  4.   
  5.     <tr bgcolor="#ffffff">  
  6.           <td align="center" valign="top"><a href="/dotAsset/${file.identifier}.${file.getExtension()}"><img src="/icon?i=${file.getExtension()}" width="16" height="16" alt="${file.getExtension()} file" border="0"></a></td>  
  7.           <td valign="top"><a href="/dotAsset/${file.identifier}.${file.getExtension()}">$file.fileName</a></td>  
  8.           <td valign="top">$math.ceil($math.div(${file.size}, 1024)) kb</td>  
  9.           <td valign="top">$UtilMethods.dateToHTMLDate($file.publishDate,'MMM d, yyyy')</td>  
  10.           <td valign="top">$!file.friendlyName</td>  
  11.     </tr>  
  12.   
  13. #end  
comments powered by Disqus