plainblack.com
Username Password
search
Bookmark and Share
Subscribe

Template Language

WebGUI has a powerful templating language built to give you maximum control over the layout of your content.

NOTES:
Both the template language and template variables are case-insensitive.

Variables
Variables are the most basic of the template commands. They are used to position pieces of content. In the examples below, please note that the words foo and bar are used as placeholders for the actual variable names that you'll use. They are not part of the template language.

Syntax: <tmpl_var foo> or <tmpl_var name="foo">

Example: <tmpl_var name>

Escaping text
Sometimes you would like to escape text to make it safe to use inside of XML and HTML tags or Javascript code. There is a built-in way to do this for variables, using the ESCAPE option inside of <tmpl_var>.

Javascript safe: <tmpl_var foo ESCAPE="JS">

XML and HTML safe: <tmpl_var foo ESCAPE="HTML">

Conditions
To programmers conditions are nothing new, but to designers they can often be confusing at first. Conditions are really just true or false questions, and if you think of them that way, you'll have no trouble at all.

Syntax: <tmpl_if foo> <tmpl_else> </tmpl_if>
Syntax: <tmpl_unless foo> <tmpl_else> </tmpl_unless>

Example: <tmpl_if isTrue> It was true!<tmpl_else> It was false! </tmpl_if>

Truth or falsehood is determined by the following rules:

  • Variables not used in this template are false.

  • Variables which are undefined are false.

  • Variables which are empty are false.

  • Variables which are equal to zero are false.

  • All other variables are true.

Loops
Loops iterate over a list of data output for each pass in the loop. Loops are slightly more complicated to use than plain variables, but are considerably more powerful.

Syntax: <tmpl_loop foo> </tmpl_loop>

Example:
<tmpl_loop users>
  Name: <tmpl_var first_name><br/>
</tmpl_loop>

Individual Loop Element Conditions
Loops come with special condition variables of their own. They are __FIRST__, __ODD__, __INNER__, and __LAST__. There is also a loop counter variable, __COUNTER__, which keeps track of how many times the loop has been executed.

Examples:

   <TMPL_LOOP FOO>
<TMPL_IF __FIRST__>
This only outputs on the first pass.
</TMPL_IF>

<TMPL_IF __ODD__>
This outputs every other pass, on the odd passes.
</TMPL_IF>

<TMPL_UNLESS __ODD__>
This outputs every other pass, on the even passes.
</TMPL_UNLESS>

<TMPL_IF __INNER__>
This outputs on passes that are neither first nor last.
</TMPL_IF>

<TMPL_IF __LAST__>
This only outputs on the last pass.
<TMPL_IF>

Loop number: <TMPL_VAR __COUNTER__>
This keeps track of how many times you have gone through the loop.
<TMPL_IF>
</TMPL_LOOP>

"Loop Has Data" Condition
Sometimes you want to test whether a loop as data in it, but not do anything with the data itself. For example, you are creating a mini-month view of a calendar, and you want to indicate which days have one or more events, but you don't need to know what the actual events are. This is possible, but due to a bug in HTML::Template, you still have to call the loop, even if you don't do anything with it.

Mini-Calendar Example:

   <TMPL_IF events>
<TMPL_LOOP events></TMPL_LOOP>
<a href="<tmpl_var dayUrl>"><tmpl_var dayMonth></a>
</TMPL_IF>


Commenting out template commands/variables

From experience and verified by http://search.cpan.org/~wonko/HTML-Template-2.91/lib/HTML/Template.pm#NOTES, commenting out a tmpl directive like:

<tmpl_var controls> 

like this:

<!-- tmpl_var controls --> 

"tmpl_var controls" will STILL be evaluated.

NOTE: This only documents WebGUI's default template language, HTML::Template. If the Template Type has been set to some other language you will need to consult the documentation for it.

Some examples have been borrowed from the HTML::Template documentation.

Keywords: html::template language Templates

Search | Most Popular | Recent Changes | Wiki Home
© 2023 Plain Black Corporation | All Rights Reserved