Velocity	
  Tips	
  and	
  Tricks	
  	
  

      Simple	
  Solu5ons	
  to	
  
      Common	
  Problems	
  
      Wed.,	
  April	
  14,	
  2010	
  
Velocity	
  ABC	
  
•  Variables:	
  
   –  #set($thumbWidth	
  =	
  55)	
  
   –  #set($varString	
  =	
  ‘ The	
  width	
  is‘)	
  
   –  #set($varString	
  =	
  “${varString}	
  ${thumbWidth}	
  px“)	
  
•  Proper5es:	
  
   –  $news.shortSummary	
  
   –  $news.expireDate	
  
•  Methods:	
  
   –  #set($_today	
  =	
  $date.getDate())	
  
   –  #set($_todayFormated	
  =	
  $date.format('yyyyMMddHHmmss',
      $_today))	
  
Velocity	
  ABC	
  
•  If	
  /	
  ElseIf	
  /	
  Else	
  statements	
  
#if($condi5on_1)	
  
	
  	
  	
  	
  Condi5on	
  1	
  was	
  met	
  
#elseif($condi5on_2)	
  
	
  	
  	
  	
  Condi5on	
  2	
  was	
  met	
  
#else	
  
	
  	
  	
  	
  	
  Another	
  condi5on	
  was	
  met	
  
#end	
  


•  AND	
  /	
  OR	
  	
  
#if($condi5on_1	
  &&	
  $condi5on_2)	
  	
  …	
  	
  #end	
  
#if($condi5on_1	
  ||	
  $condi5on_2)	
  	
  …	
  #end	
  
Velocity	
  ABC	
  
•  Lists	
  
#set($mylist	
  =	
  [])	
  
$mylist.add("asd")	
  
$mylist.add(123)	
  
$mylist.get(0)	
  

•  Maps	
  
#set($contentList	
  =	
  $!{contents.getEmptyList()})	
  	
  
#set($item	
  =	
  $!{contents.getEmptyMap()})	
  	
  
#set($_dummy	
  =	
  $item.put('guid',	
  $!content.get('iden5fier')))	
  	
  
#set($_dummy	
  =	
  $item.put('5tle',	
  $content.get($5tleField)))	
  
#set($_dummy	
  =	
  $!contentList.add($item))	
  	
  
Velocity	
  ABC	
  
•  Lists	
  and	
  Foreach	
  	
  
#foreach($item	
  in	
  $myList)	
  
	
  	
  	
  $item.5tle	
  <br/>	
  	
  
#end	
  


•  Velocity	
  Comments	
  
##	
  Commen5ng	
  one	
  line	
  
#*	
  	
  	
  Commen5ng	
  
	
  	
  	
  	
  	
  	
  	
  Mul5ple	
  lines	
  

*#	
  
Velocity	
  and	
  dotCMS	
  
•  Useful	
  variables:	
  
   –  $EDIT_MODE	
  
   –  $CONTAINER_NUM_CONTENTLETS	
  
   –  $VTLSERVLET_URI	
  
Velocity	
  and	
  dotCMS	
  
•  View	
  Tools	
  are	
  configured	
  in:	
  
    –  dotCMSWEB-­‐INFtoolbox.xml	
  


•  Three	
  types	
  of	
  view	
  tools:	
  
    –  Velocity	
  view	
  tools:	
  org.apache.velocity.tools….	
  
    –  dotCMS	
  view	
  tools:	
  
       com.dotmarke5ng.viewtools….	
  
    –  Customized	
  view	
  tools:	
  plugins	
  
Velocity	
  and	
  dotCMS	
  

•  Velocity	
  View	
  Tool	
  
   samples:	
  

    –  MathTool	
  
    –  ListTool	
  
    –  DateTool	
  



  hpp://velocity.apache.org/tools/devel/javadoc/   	
  
Velocity	
  and	
  dotCMS	
  
•  dotCMS	
  View	
  Tool	
  
   samples:	
  
    –  CategoriesWebAPI	
  
    –  WebAPI	
  
    –  ContentsWebAPI	
  




  hpp://www.dotcms.org/api/     	
  
Velocity	
  and	
  dotCMS	
  
•  org.apache.velocity.tools.generic.MathTool:	
  
   –  java.lang.Number	
  add(java.lang.Object	
  num1,	
  
      java.lang.Object	
  num2)	
  
   –  java.lang.Number	
  div(java.lang.Object	
  num1,	
  
      java.lang.Object	
  num2)	
  
   –  java.lang.Double	
  getRandom()	
  
   –  java.lang.Number	
  max(java.lang.Object	
  num1,	
  
      java.lang.Object	
  num2)	
  
   –  java.lang.Number	
  sub(java.lang.Object	
  num1,	
  
      java.lang.Object	
  num2)	
  	
  
Velocity	
  and	
  dotCMS	
  
•  org.apache.velocity.tools.generic.ListTool:	
  
   –  java.lang.Boolean	
  contains(java.lang.Object	
  list,	
  
      java.lang.Object	
  element)	
  	
  
   –  java.lang.Object	
  get(java.lang.Object	
  list,	
  int	
  index)	
  	
  
   –  java.lang.Boolean	
  isEmpty(java.lang.Object	
  list)	
  	
  
   –  java.lang.Integer	
  size(java.lang.Object	
  list)	
  	
  
Velocity	
  and	
  dotCMS	
  
•  org.apache.velocity.tools.generic.DateTool:	
  
   –  java.u5l.Date	
  getDate()	
  
   –  java.lang.Integer	
  getDay(java.lang.Object	
  date)	
  
   –  java.lang.Integer	
  getMonth(java.lang.Object	
  date)	
  
   –  sta5c	
  java.u5l.Date	
  getSystemDate()	
  
   –  java.lang.Integer	
  getYear(java.lang.Object	
  date)	
  
   –  java.u5l.Date	
  toDate(java.lang.Object	
  obj)	
  
   –  java.lang.String	
  toString()	
  
Velocity	
  and	
  dotCMS	
  
•  CategoriesWebAPI:	
  
   –  List<Category>	
  getChildrenCategoriesByKey(String	
  
      key)	
  
   –  Category	
  getCategoryByKey(String	
  key)	
  
   –  Category	
  getCategoryByName(String	
  name)	
  
   –  List<Category>	
  getChildrenCategories(Category	
  cat)	
  
   –  List<Category>	
  getChildrenCategories(Inode	
  inode)	
  
   –  List<Category>	
  getChildrenCategories(String	
  inode)	
  
   –  String	
  getCategoryKeyByContentlet(String	
  
      contentletInode)	
  
Velocity	
  and	
  dotCMS	
  
•  WebAPI:	
  
   –  int	
  parseInt(String	
  num)	
  
   –  int	
  parseInt(int	
  num)	
  
   –  String	
  toString(long	
  num)	
  
   –  boolean	
  isSet(String	
  input)	
  
   –  List<String>	
  splitString(String	
  str,	
  String	
  sep)	
  
   –  String	
  encodeURL(String	
  url)	
  
   –  List<Contentlet>	
  getContentletsByCategory(String	
  
      catInode)	
  
   –  	
  String	
  getContentIden5fier(String	
  parsePath)	
  
   –  String	
  getUserFullName()	
  
Velocity	
  and	
  dotCMS	
  
•  ContentsWebAPI:	
  
   –  Contentlet	
  getContentByInode(String	
  inode)	
  
   –  Structure	
  getStructureByType(String	
  structureType)	
  
   –  Structure	
  getStructureByInode(String	
  structureInode)	
  
   –  Field	
  getFieldByName(Structure	
  st,	
  String	
  fieldName)	
  
   –  List<Contentlet>	
  getContents(String	
  structureType,	
  
      String	
  categoryName)	
  
   –  List<Category>	
  getContentletCategories(String	
  inode)	
  	
  
   –  List	
  getEmptyList()	
  
Examples	
  
•    Dynamic	
  Banner	
  
•    Category	
  Example	
  
•    News	
  Pull	
  
•    Simple	
  Year	
  Archive	
  

Velocity tips and tricks

  • 1.
    Velocity  Tips  and  Tricks     Simple  Solu5ons  to   Common  Problems   Wed.,  April  14,  2010  
  • 2.
    Velocity  ABC   • Variables:   –  #set($thumbWidth  =  55)   –  #set($varString  =  ‘ The  width  is‘)   –  #set($varString  =  “${varString}  ${thumbWidth}  px“)   •  Proper5es:   –  $news.shortSummary   –  $news.expireDate   •  Methods:   –  #set($_today  =  $date.getDate())   –  #set($_todayFormated  =  $date.format('yyyyMMddHHmmss', $_today))  
  • 3.
    Velocity  ABC   • If  /  ElseIf  /  Else  statements   #if($condi5on_1)          Condi5on  1  was  met   #elseif($condi5on_2)          Condi5on  2  was  met   #else            Another  condi5on  was  met   #end   •  AND  /  OR     #if($condi5on_1  &&  $condi5on_2)    …    #end   #if($condi5on_1  ||  $condi5on_2)    …  #end  
  • 4.
    Velocity  ABC   • Lists   #set($mylist  =  [])   $mylist.add("asd")   $mylist.add(123)   $mylist.get(0)   •  Maps   #set($contentList  =  $!{contents.getEmptyList()})     #set($item  =  $!{contents.getEmptyMap()})     #set($_dummy  =  $item.put('guid',  $!content.get('iden5fier')))     #set($_dummy  =  $item.put('5tle',  $content.get($5tleField)))   #set($_dummy  =  $!contentList.add($item))    
  • 5.
    Velocity  ABC   • Lists  and  Foreach     #foreach($item  in  $myList)        $item.5tle  <br/>     #end   •  Velocity  Comments   ##  Commen5ng  one  line   #*      Commen5ng                Mul5ple  lines   *#  
  • 6.
    Velocity  and  dotCMS   •  Useful  variables:   –  $EDIT_MODE   –  $CONTAINER_NUM_CONTENTLETS   –  $VTLSERVLET_URI  
  • 7.
    Velocity  and  dotCMS   •  View  Tools  are  configured  in:   –  dotCMSWEB-­‐INFtoolbox.xml   •  Three  types  of  view  tools:   –  Velocity  view  tools:  org.apache.velocity.tools….   –  dotCMS  view  tools:   com.dotmarke5ng.viewtools….   –  Customized  view  tools:  plugins  
  • 8.
    Velocity  and  dotCMS   •  Velocity  View  Tool   samples:   –  MathTool   –  ListTool   –  DateTool   hpp://velocity.apache.org/tools/devel/javadoc/  
  • 9.
    Velocity  and  dotCMS   •  dotCMS  View  Tool   samples:   –  CategoriesWebAPI   –  WebAPI   –  ContentsWebAPI   hpp://www.dotcms.org/api/  
  • 10.
    Velocity  and  dotCMS   •  org.apache.velocity.tools.generic.MathTool:   –  java.lang.Number  add(java.lang.Object  num1,   java.lang.Object  num2)   –  java.lang.Number  div(java.lang.Object  num1,   java.lang.Object  num2)   –  java.lang.Double  getRandom()   –  java.lang.Number  max(java.lang.Object  num1,   java.lang.Object  num2)   –  java.lang.Number  sub(java.lang.Object  num1,   java.lang.Object  num2)    
  • 11.
    Velocity  and  dotCMS   •  org.apache.velocity.tools.generic.ListTool:   –  java.lang.Boolean  contains(java.lang.Object  list,   java.lang.Object  element)     –  java.lang.Object  get(java.lang.Object  list,  int  index)     –  java.lang.Boolean  isEmpty(java.lang.Object  list)     –  java.lang.Integer  size(java.lang.Object  list)    
  • 12.
    Velocity  and  dotCMS   •  org.apache.velocity.tools.generic.DateTool:   –  java.u5l.Date  getDate()   –  java.lang.Integer  getDay(java.lang.Object  date)   –  java.lang.Integer  getMonth(java.lang.Object  date)   –  sta5c  java.u5l.Date  getSystemDate()   –  java.lang.Integer  getYear(java.lang.Object  date)   –  java.u5l.Date  toDate(java.lang.Object  obj)   –  java.lang.String  toString()  
  • 13.
    Velocity  and  dotCMS   •  CategoriesWebAPI:   –  List<Category>  getChildrenCategoriesByKey(String   key)   –  Category  getCategoryByKey(String  key)   –  Category  getCategoryByName(String  name)   –  List<Category>  getChildrenCategories(Category  cat)   –  List<Category>  getChildrenCategories(Inode  inode)   –  List<Category>  getChildrenCategories(String  inode)   –  String  getCategoryKeyByContentlet(String   contentletInode)  
  • 14.
    Velocity  and  dotCMS   •  WebAPI:   –  int  parseInt(String  num)   –  int  parseInt(int  num)   –  String  toString(long  num)   –  boolean  isSet(String  input)   –  List<String>  splitString(String  str,  String  sep)   –  String  encodeURL(String  url)   –  List<Contentlet>  getContentletsByCategory(String   catInode)   –   String  getContentIden5fier(String  parsePath)   –  String  getUserFullName()  
  • 15.
    Velocity  and  dotCMS   •  ContentsWebAPI:   –  Contentlet  getContentByInode(String  inode)   –  Structure  getStructureByType(String  structureType)   –  Structure  getStructureByInode(String  structureInode)   –  Field  getFieldByName(Structure  st,  String  fieldName)   –  List<Contentlet>  getContents(String  structureType,   String  categoryName)   –  List<Category>  getContentletCategories(String  inode)     –  List  getEmptyList()  
  • 16.
    Examples   •  Dynamic  Banner   •  Category  Example   •  News  Pull   •  Simple  Year  Archive