Templates
Cloud CMS supports the usage of templates at various points to generate presentation and output for things like
emails, PDFs, web page components and more.
Cloud CMS supports two template engines - Handlebars
and Freemarker. In general, we recommend using Handlebars
since the syntax is a bit easier. This document covers Handlebars and describes helper functions available in
Handlebars that make processing simpler.
Model Variables
The following describes the internal structure of model variables based on their type.
Helper Functions
each
Allows for iteration of a list of items.
Syntax:
{{each array}}
Example - assume we have the following content:
{
"title": "My Article",
"description": My Article Description",
"tags": ["tag1", "tag2"]
}
And the following template:
Title: {{title}}
<br/>
Description: {{description}}
<br/>
{{#each tags}}
Tag: {{this}}
<br/>
{{/each}}
This will render:
Title: My Article
Description: My Article Description
Tag: tag1
Tag: tag2
if
Conditionally executes a block.
Syntax:
{{#if boolean}}
this shows if true
{{/if}}
Example - assume we have the following:
{
"_doc": "GUID1",
"title": "My Article",
"description": My Article Description",
"download": true
}
And the following template:
Title: {{title}}
<br/>
Description: {{description}}
<br/>
{{#if download}}
<a href="/download/{{_doc}}.txt">Download</a>
{{/if}}
This will render:
Title: My Article
Description: My Article Description
Download
unless
Conditionally executes a block.
Syntax:
{{#unless boolean}}
this shows if false
{{/unless}}
Example - assume we have the following:
{
"_doc": "GUID1",
"title": "My Article",
"description": My Article Description",
"preventDownload": false
}
And the following template:
Title: {{title}}
<br/>
Description: {{description}}
<br/>
{{#unless preventDownload}}
<a href="/download/{{_doc}}.txt">Download</a>
{{/unless}}
This will render:
Title: My Article
Description: My Article Description
Download
capitalizeFirst
Capitalizes the first letter of a given value.
Syntax:
{{capitalizeFirst text}}
Example - assume we have the following:
{
"title": "my article"
}
And the following template:
Title: {{capitalizeFirst title}}
This will render:
Title: My article
center
Centers the value in a field of a given width.
Syntax:
{{center text}}
Example - assume we have the following:
{
"title": "my article"
}
And the following template:
Title: --{{center title size=20 pad=" "}}--
This will render:
Title: -- my article --
Note that "my article" has a length of 10.
The displayed value has a length of 20 with padding of 5 spaces on either side.
cut
Removes all values of arg from the given string.
Syntax:
{{cut text textToCut}}
Example - assume we have the following:
{
"title": "my article"
}
And the following template:
Title: {{cut title " "}}
This will render:
Title: myarticle
defaultIfEmpty
Emits the given default if the value is empty.
Syntax:
{{defaultIfEmpty value textIfEmpty}}
Example - assume we have the following:
{
"title": ""
}
And the following template:
Title: {{defaultIfEmpty title "nothing to see here"}}
This will render:
Title: nothing to see here
join
Joins multiple string fragments together.
Syntax:
{{join value1 value2 value3 ... valueN joinText}}
Example - assume we have the following:
{
"path": "/folder1/folder2",
"subfolder": "folder3",
"filename": "myfile.txt"
}
And the following template:
Path: {{join path subfolder filename "/"}}
This will render:
Path: /folder1/folder2/folder3/myfile.txt
ljust
Syntax:
{{ljust text size pad}}
Example - assume we have the following:
{
"title": "my article"
}
And the following template:
Title: --{{ljust title size=20 pad=" "}}--
This will render:
Title: --my article --
rjust
Syntax:
{{rjust text size pad}}
Example - assume we have the following:
{
"title": "my article"
}
And the following template:
Title: --{{rjust title size=20 pad=" "}}--
This will render:
Title: -- my article--
substring
Syntax:
{{substring value startIndex [endIndex]}}
Example - assume we have the following:
{
"title": "my article"
}
And the following template:
Title: {{substring title 0 2}}
This will render:
Title: my
lower
Syntax:
{{lower value}}
Example - assume we have the following:
{
"title": "My Article"
}
And the following template:
Title: {{lower title}}
This will render:
Title: my article
upper
Syntax:
{{upper value}}
Example - assume we have the following:
{
"title": "My Article"
}
And the following template:
Title: {{upper title}}
This will render:
Title: MY ARTICLE
slugify
non-word characters removed (non-alphanumerics), has spaces converted to hyphens and has leading and trailing
whitespace removed.
Syntax:
{{slugify value}}
Example - assume we have the following:
{
"_doc": "GUID1",
"title": "My Article",
"body": "...",
"seo": "My Article on Quantum Physics"
}
And the following template:
<a href="/article/view/{{_doc}}/{{seo}}>View Article</a>
This will render:
<a href="/article/view/GUID1/my-article-on-quantum-physics">View Article</a>
stringFormat
Syntax:
{{stringFormat string param0 param1 ... paramN}}
Example - assume we have the following:
{
"title": "My Article",
"author": "Joe Smith",
"email": "joe@smith.com"
}
And the following template:
{{stringFormat "%s by %s (%s)" title author email}}
This will render:
My Article by Joe Smith (joe@smith.com)
stripTags
Syntax:
{{stripTags value}}
Example - assume we have the following:
{
"title": "My Article <b>is awesome</b>"
}
And the following template:
{{stripTags title}}
This will render:
My Article is awesome
capitalize
Syntax:
{{capitalize value}}
Example - assume we have the following:
{
"title": "hello my friend, welcome to the machine"
}
And the following template:
{{capitalize title}}
This will render:
Hello My Friend, Welcome To The Machine
abbreviate
Truncated strings will end with a translatable ellipsis sequence ("...").
Syntax:
{{abbreviate value maxLength}}
Example - assume we have the following:
{
"title": "hello my friend, welcome to the machine"
}
And the following template:
{{abbreviate title 15}}
This will render:
hello my friend...
replace
replacement sequence.
Syntax:
{{replace value original replacement}}
Example - assume we have the following:
{
"title": "hello my friend, welcome to the machine"
}
And the following template:
{{replace title "friend" "son"}}
This will render:
hello my son, welcome to the machine
dateFormat
Syntax:
{{dateFormat date format}}
Format parameters is one of:
- "full": full date format. For example: Tuesday, June 19, 2012
- "long": long date format. For example: June 19, 2012
- "medium": medium date format. For example: Jun 19, 2012
- "short": short date format. For example: 6/19/12
- "pattern": a date pattern.
Otherwise, the default formatter will be used.
Example - assume we have the following:
{
"title": "My Article",
"_system": {
"created_on": {
}
}
}
And the following template:
{{title}} published on {{dateFormat _system.created_on.ms "full"}}
This will render:
My Article published on Tuesday, June 19, 2012
numberFormat
Syntax:
{{numberFormat number format}}
Format parameters is one of:
- "integer": the integer number format
- "percent": the percent number format
- "currency": the decimal number format
- "pattern": a decimal pattern.
Otherwise, the default formatter will be used.
More options:
- groupingUsed: Set whether or not grouping will be used in this format.
- maximumFractionDigits: Sets the maximum number of digits allowed in the fraction portion of a number
- maximumIntegerDigits: Sets the maximum number of digits allowed in the integer portion of a number
- minimumFractionDigits: Sets the minimum number of digits allowed in the fraction portion of a number
- minimumIntegerDigits: Sets the minimum number of digits allowed in the integer portion of a number
- parseIntegerOnly: Sets whether or not numbers should be parsed as integers only
- roundingMode: Sets the {@link java.math.RoundingMode} used in this NumberFormat.
Example - assume we have the following:
{
"title": "ColecoVision Corporation",
"index": "NASDAQ",
"price": 10.5712
}
And the following template:
{{title}} is trading at {{numberFormat price "currency" maximumFractionDigits=2}}
This will render:
ColecoVision Corporation is trading at $10.57
now
Syntax:
{{now format}}
Format parameters is one of:
- "full": full date format. For example: Tuesday, June 19, 2012
- "long": long date format. For example: June 19, 2012
- "medium": medium date format. For example: Jun 19, 2012
- "short": short date format. For example: 6/19/12
- "pattern": a date pattern
Example - assume we have the following:
{
"title": "ColecoVision Corporation",
"index": "NASDAQ",
"price": 10.5712
}
And the following template:
{{title}} is trading at {{numberFormat price "currency" maximumFractionDigits=2}} on {{now "full"}}.
This will render:
ColecoVision Corporation is trading at $10.57 on Friday, August 28, 2015.