assign
TEMPLATE ============================= { assign var="test" value="this is a test variable"|upper } The value of $test is { $test }. OUTPUT ============================= The value of $test is THIS IS A TEST VARIABLE.
config_load
EXAMPLE ============================= { config_load file="config.conf" } { #variable# }
foreach/foreachelse
foreach
. It will loop through an array and return the output.
PHP ============================= $tpl->assign("contacts", array( array("phone" => "1", "fax" => "2", "cell" => "3"), array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234") )); TEMPLATE ============================= { foreach value=contact from=$contacts } { foreach key=key value=item from=$contact } { $key }: { $item }<br> { /foreach } { /foreach } OUTPUT ============================= phone: 1<br> fax: 2<br> cell: 3<br> phone: 555-4444<br> fax: 555-3333<br> cell: 760-1234<br>
for
for
. You can specify a starting integer, an ending integer, and a step value.
EXAMPLE ============================= { for start=0 stop=10 step=2 value=current } We are on number { $current } { /for } { for start=0 stop=1 step=0.2 value=current } We are on number { $current } { /for } OUTPUT ============================= We are on number 0 We are on number 2 We are on number 4 We are on number 6 We are on number 8 We are on number 0 We are on number 0.2 We are on number 0.4 We are on number 0.6 We are on number 0.8
include
include
to begin with. Included files are also compiled and optionally saved for faster execution.
EXAMPLE ============================= { include file="template.tpl" } { include file=$page }
insert
PHP
=============================
function insert_stuffandjunk($params, &$tpl) {
return $tpl->fetch('template.tpl','sidebar|template');
}
function insert_othercrap($params, &$tpl) {
return "random text: " . $params["var"];
}
TEMPLATE
=============================
{ insert name="stuffandjunk" }
{ insert name="othercrap" var="hi" }
OUTPUT
=============================
This is the contents of template.tpl
random text: hi
if/elseif/else
Much as the name describes, this is the equivalent to PHP's if
statement. In fact, it offers exactly the same flexibility, if not more. Every if
, though, must be paired with an /if
. Additionally, else
and elseif
are permitted. The comparison tokens allowed are !
, %
, !==
, ==
, ===
, >
, <
, !=
, <>
, <<
, >>
, <=
, >=
,&&
, ||
, ^
, &
, ~
, (
, )
, ,
, +
, -
, *
, /
, @
, eq
, ne
, neq
, lt
, le
, lte
, gt
, ge
, gte
, and
, or
, not
, mod
. It is important to note that the comparison operators must be separated from the variables by at least one space.
The is
token is currently not supported.
EXAMPLES ============================= {if $name eq "Fred"} Welcome Sir. {elseif $name eq "Wilma"} Welcome Ma'am. {else} Welcome, whatever you are. {/if} {* an example with "or" logic *} {if $name eq "Fred" or $name eq "Wilma"} ... {/if} {* same as above *} {if $name == "Fred" || $name == "Wilma"} ... {/if} {* the following syntax will NOT work, conditional qualifiers must be separated from surrounding elements by spaces *} {if $name=="Fred" || $name=="Wilma"} ... {/if} {* parenthesis are allowed *} {if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#} ... {/if} {* you can also embed php function calls *} {if count($var) gt 0} ... {/if}
ldelim/rdelim
TEMPLATE ============================= Here is an example: { ldelim } config_load file="config.conf" { rdelim } OUTPUT ============================= Here is an example: { config_load file="config.conf" }
literal
EXAMPLE ============================= { literal } Here is an example: { config_load file="config.conf" } { /literal }
php
EXAMPLE ============================= { php } echo "Hello to " . $this->get_vars('username') . "<BR>"; { /php }
section/sectionelse
section
section
PHP ============================= $player_id = array(1,2,3); $tpl->assign('player_id',$player_id); $player_name = array('Panama Jack','Tarnus Harten','Goober'); $tpl->assign('player_name',$player_name); TEMPLATE ============================= {section name=player_number loop=$player_id} <p> Player ID: {$player_id[player_number]}<br /> Player Name: {$player_name[player_number]} </p> {/section} OUTPUT ============================= <p> Player ID: 1<br /> Player Name: Panama Jack </p> <p> Player ID: 2<br /> Player Name: Tarnus Harten </p> <p> Player ID: 3<br /> Player Name: Goober </p>
switch/case
switch
function. It will execute a different block of code depending on the given value.
switch
case
TEMPLATE ============================= { switch from=$variable } { case value="case1" } This is case number one. { case value="case2" } This is case number 2 { case } This is the default. Nothing matched above. { /switch }
while
Much like the name indicates, this function behaves very similar to PHP's while
function. It will execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE.
The while function operates similar to the if
statement. It offers exactly the same flexibility while looping through the nested statements(s). Every while
must be paired with an /while
. The comparison tokens allowed are !
, %
, !==
, ==
, ===
, >
, <
, !=
, <>
, <<
, >>
, <=
, >=
,&&
, ||
, ^
, &
, ~
, (
, )
, ,
, +
, -
, *
, /
, @
, eq
, ne
, neq
, lt
, le
, lte
, gt
, ge
, gte
, and
, or
, not
, mod
. It is important to note that the comparison operators must be separated from the variables by at least one space.
TEMPLATE ============================= {assign var="counter" value="0"} {while $counter < 10} [{$counter}]
{ math equation="x + 1" x=$counter assign="counter" } {/while} OUTPUT ============================= [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]