Body
A template body can be written in plain HTML or combination of HTML and Velocity markup. The available objects for the Velocity context are described in API for templates
Velocity examples (7.1)
Title of the page
$resource.getTitle()
Get a service object
#set ($resources = $services.getResources())
#set ($folders = $services.getFolders())
Display paragraph
$resource.getParagraph("HEADER").text()
Array and random selection
#set( $randomStyles = ["one", "two", "three", "four", "five", "six", "seven", "eight"] )
Random access (assuming that an instance of java.util.Random in the Velocity context with name random)
<body id="home" class="$randomStyles.get($random.nextInt(8))">
Create a content index
#set($folderResource= $resources.resourceForPathSecure("/Events")) #set($asc = $enums.of("ccc.types.SortOrder", "ASC")) #set($folder = $folders.getChildrenPaged($folderResource.getId(),"",$asc,1,20)) #foreach ($pageRS in $folder) #if($pageRS.isIncludeInMainMenu()) #set($page = $pages.pageDelta($pageRS.getId())) <h2><a href="$pageRS.getAbsolutePath()">$pageRS.getTitle()</a></h2> <p>$page.getParagraph("Summary").text()</p> <ul class="eventlist"> #set($rawDate = $page.getParagraph("Date").date()) <li>Date: $dateTool.format('d MMMMM yyyy', $rawDate)</li> <li>Time: $page.getParagraph("Time").text()</li> </ul> #end #end
Display search results (for 'search' resource's template)
#set($resources = $services.getResources()) #foreach($hit in $result.hits()) #set ($hitResource = $resources.resource($hit)) $hitResource.getTitle()</br> #end <form name="search" action="$resource.name()"> <input name="q" autocomplete="off" value="$terms"/> <input type="submit" value="Search" name="go"/> </form> Shown Hits: $!result.hits().size() - Total: $!result.totalResults()</br> #set ($pages = ($result.totalResults()+9)/10) #if ( $result.totalResults() > 0) #foreach($page in [1..$pages]) #set ($linkNumber = ($page+-1)) #if ($linkNumber == $pageNumber) <b>$page</b> #elseif ($linkNumber != $pageNumber) <a href="$resource.name()?q=$terms&p=$linkNumber">$page</a> #end #end #end
Display optional paragraph
#macro( optional $name $startText $endText)
#foreach( $para in $resource.getParagraphs() )
#if ($para.name() == $name && $para.text())
$startText
$para.text()
$endText
#end
#end
#end
Normal $resource.paragraph("NAME").text() causes an exception in case there is no paragraph with matching name.
Following macro takes three parameters. $name is for the paragraph name, $startText is the text to be appended before the paragraph text, $endText is the text to be appended after the paragraph.
Usage:
#optional('PanelOne_Custom' '<div id="panel">' '</div>' )
Display breadcrumb
#macro ( breadcrumb) #foreach ($element in $resource.getAbsolutePath().substring(9).split("/")) $element <br/> #end #end
Include another resource
#include("/assets/templates/ash_header.vm")
Parse another resource
#parse("/assets/templates/ash_header.vm")
Read metadata value with default value
#macro(getStyle) #if(!$resource.getMetadatum("bodyId")) default #else $resource.getMetadatum("bodyId") #end #end
#getStyle
Display current year
#macro (currentYear) #set ($cal = $calendar.getInstance()) $cal.get(1) #end
or
$dateTools.get('yyyy')
Display image type paragraph
#macro( optionalImage $name) #foreach( $para in $resource.getParagraphs() ) #if ($para.name() == $name && $para.text()) #set($imageResource = $resources.resource($uuid.fromString($para.text()))) $imageResource.getAbsolutePath() #end #end #end