Jump To: Support > KB > Template
TemplateEngine
TemplateEngine is used as the core of a number of Precedence software products such as FileSurfer and NetManager Intranet. It is also used for many of our custom developments.
TemplateEngine allows the look and feel of dynamic webpages to be separated from the software that generates the pages. This means that the user interface can be significantly altered without any specific programming skills beyond the HTML and CSS necessary for the design work.
Introduction
A template HTML file is loaded by the software and then processed before sending the output to the web-browser. Most of the HTML/text contained will be sent through without being touched, however some characters have special meaning. The software itself creates what is known as a context. This contains the dynamic parts of the website. The context and template are combined to form the output, i.e. the template is static, but the context differs from page to page. More importantly, the information that the context contains will differ between different software. Therefore, this page will not document the details of the context, merely how it will be used.
Basic substitution
Text surrounded by [[ and ]] will be used to look up an item from the context.
Let us assume the template contains:
<title>[[title]]</title>
and that the context specifies that
title is
My Web Page. This will output
<title>My Web Page</title>
to the browser.
Multi-dimensional arrays can be access by separating the subscripts with commas, e.g. [[person,name]]] will be replaced by context['person']['name'].
Conditional substitution
The character pairs {{ and }} are used to separate out blocks of text. You may use two question marks to define a conditions when this block will be displayed.
The simplest is whether a name is set in the context:
{{?unread?You have unread emails}}
If unread is unset or set to zero, then this will output nothing. However if unread has a value, the string You have unread emails will be output.
{{}} sections can span multiple lines:
{{?option?<tr>
<td>line</td>
</tr>
}}
Remember you can also include normal substitions (and even other nested conditional blocks):
{{?error?<h1>Error: [[error]]</h1>}}
You can use ! to reverse the logic:
{{?!unread?You have read all your email}}
If/Then/Else can be used by surrounding the Then and Else blocks with more {{ and }} characters. For example:
This option is currently {{?option?{{enabled}}{disabled}}}}
Always remember to close off your [[ and {{ tags correctly.
More complicated conditionals
You can do numeric comparisons:
You have [[unread]] email{{!?unread==1?s}}
- == equals
- < less than
- > greater than
- <= less than or equal to
- >= greater than or equal to
And text comparisons:
It's a {{?day=='weds''?Wednesday}}
(less than or greater than will refer to alphabetical order for text).
The special := comparison is used to test when a string contains a substring:
{{?name:=s?Your name contains the letter S!}}
Calling functions
PHP functions can be called by using the * character in your context lookups:
[[func*name]]
will be replaced by value of function func(name?).
Functions can be nested, e.g.
[[func1*func2*func3*name]]
will be replaced by func1(func2(func3([name]])))
Looping over an array
Sometimes the context contains lists of data within a single name. These are looped over by using | around the name of the content component:
My friends are called:
<ul>
{{|friends|{{<li>[[friends]]</li>))))
</ul>