Velocity	
  Tips	
  and	
  Tricks	
  	
  

      Simple	
  Solu5ons	
  to	
  
      Common	
  Problems	
  
      Wed.,	
  ...
Velocity	
  ABC	
  
•  Variables:	
  
   –  #set($thumbWidth	
  =	
  55)	
  
   –  #set($varString	
  =	
  ‘ The	
  width	...
Velocity	
  ABC	
  
•  If	
  /	
  ElseIf	
  /	
  Else	
  statements	
  
#if($condi5on_1)	
  
	
  	
  	
  	
  Condi5on	
  1...
Velocity	
  ABC	
  
•  Lists	
  
#set($mylist	
  =	
  [])	
  
$mylist.add("asd")	
  
$mylist.add(123)	
  
$mylist.get(0)	
...
Velocity	
  ABC	
  
•  Lists	
  and	
  Foreach	
  	
  
#foreach($item	
  in	
  $myList)	
  
	
  	
  	
  $item.5tle	
  <br/...
Velocity	
  and	
  dotCMS	
  
•  Useful	
  variables:	
  
   –  $EDIT_MODE	
  
   –  $CONTAINER_NUM_CONTENTLETS	
  
   –  ...
Velocity	
  and	
  dotCMS	
  
•  View	
  Tools	
  are	
  configured	
  in:	
  
    –  dotCMSWEB-­‐INFtoolbox.xml	
  


•  T...
Velocity	
  and	
  dotCMS	
  

•  Velocity	
  View	
  Tool	
  
   samples:	
  

    –  MathTool	
  
    –  ListTool	
  
  ...
Velocity	
  and	
  dotCMS	
  
•  dotCMS	
  View	
  Tool	
  
   samples:	
  
    –  CategoriesWebAPI	
  
    –  WebAPI	
  
...
Velocity	
  and	
  dotCMS	
  
•  org.apache.velocity.tools.generic.MathTool:	
  
   –  java.lang.Number	
  add(java.lang.O...
Velocity	
  and	
  dotCMS	
  
•  org.apache.velocity.tools.generic.ListTool:	
  
   –  java.lang.Boolean	
  contains(java....
Velocity	
  and	
  dotCMS	
  
•  org.apache.velocity.tools.generic.DateTool:	
  
   –  java.u5l.Date	
  getDate()	
  
   –...
Velocity	
  and	
  dotCMS	
  
•  CategoriesWebAPI:	
  
   –  List<Category>	
  getChildrenCategoriesByKey(String	
  
     ...
Velocity	
  and	
  dotCMS	
  
•  WebAPI:	
  
   –  int	
  parseInt(String	
  num)	
  
   –  int	
  parseInt(int	
  num)	
 ...
Velocity	
  and	
  dotCMS	
  
•  ContentsWebAPI:	
  
   –  Contentlet	
  getContentByInode(String	
  inode)	
  
   –  Stru...
Examples	
  
•    Dynamic	
  Banner	
  
•    Category	
  Example	
  
•    News	
  Pull	
  
•    Simple	
  Year	
  Archive	...
Upcoming SlideShare
Loading in …5
×

Velocity tips and tricks

2,252 views

Published on

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.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,252
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
37
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Velocity tips and tricks

  1. 1. Velocity  Tips  and  Tricks     Simple  Solu5ons  to   Common  Problems   Wed.,  April  14,  2010  
  2. 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. 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. 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. 5. Velocity  ABC   •  Lists  and  Foreach     #foreach($item  in  $myList)        $item.5tle  <br/>     #end   •  Velocity  Comments   ##  Commen5ng  one  line   #*      Commen5ng                Mul5ple  lines   *#  
  6. 6. Velocity  and  dotCMS   •  Useful  variables:   –  $EDIT_MODE   –  $CONTAINER_NUM_CONTENTLETS   –  $VTLSERVLET_URI  
  7. 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. 8. Velocity  and  dotCMS   •  Velocity  View  Tool   samples:   –  MathTool   –  ListTool   –  DateTool   hpp://velocity.apache.org/tools/devel/javadoc/  
  9. 9. Velocity  and  dotCMS   •  dotCMS  View  Tool   samples:   –  CategoriesWebAPI   –  WebAPI   –  ContentsWebAPI   hpp://www.dotcms.org/api/  
  10. 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. 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. 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. 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. 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. 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. 16. Examples   •  Dynamic  Banner   •  Category  Example   •  News  Pull   •  Simple  Year  Archive  

×