16
Velocity Tips and Tricks Simple Solu5ons to Common Problems Wed., April 14, 2010

Velocity tips and tricks

  • Upload
    dotcms

  • View
    2.203

  • Download
    3

Embed Size (px)

DESCRIPTION

Presentation from dotCMS Boot Camp 2010. This covers simple solutions to common problems in Velocity. Learn basic velocity syntax and frequently used dotCMS view tools.

Citation preview

Page 1: Velocity tips and tricks

Velocity  Tips  and  Tricks    

Simple  Solu5ons  to  Common  Problems  

Wed.,  April  14,  2010  

Page 2: Velocity tips and tricks

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))  

Page 3: Velocity tips and tricks

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  

Page 4: Velocity tips and tricks

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))    

Page 5: Velocity tips and tricks

Velocity  ABC  

•  Lists  and  Foreach    #foreach($item  in  $myList)  

     $item.5tle  <br/>    

#end  

•  Velocity  Comments  ##  Commen5ng  one  line  #*      Commen5ng  

             Mul5ple  lines  

*#  

Page 6: Velocity tips and tricks

Velocity  and  dotCMS  

•  Useful  variables:  – $EDIT_MODE  – $CONTAINER_NUM_CONTENTLETS  – $VTLSERVLET_URI  

Page 7: Velocity tips and tricks

Velocity  and  dotCMS  

•  View  Tools  are  configured  in:  – \dotCMS\WEB-­‐INF\toolbox.xml  

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

– Customized  view  tools:  plugins  

Page 8: Velocity tips and tricks

Velocity  and  dotCMS  

•  Velocity  View  Tool  samples:  

– MathTool  –  ListTool  –  DateTool  

hpp://velocity.apache.org/tools/devel/javadoc/  

Page 9: Velocity tips and tricks

Velocity  and  dotCMS  

•  dotCMS  View  Tool  samples:  –  CategoriesWebAPI  

– WebAPI  –  ContentsWebAPI  

hpp://www.dotcms.org/api/  

Page 10: Velocity tips and tricks

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)    

Page 11: Velocity tips and tricks

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)    

Page 12: Velocity tips and tricks

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()  

Page 13: Velocity tips and tricks

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)  

Page 14: Velocity tips and tricks

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()  

Page 15: Velocity tips and tricks

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()  

Page 16: Velocity tips and tricks

Examples  

•  Dynamic  Banner  •  Category  Example  

•  News  Pull  •  Simple  Year  Archive