![]() |
Flat-Frog - the faster compiling PHP template engine |
assign
TEMPLATE ============================= <% assign name="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>With the use of name and the two loop variables <loopName>_index and <loopName>_count
PHP ============================= $tpl->assign('list', array('one','two','three','four','five','six','seven','eight','nine')); TEMPLATE ============================= <% foreach value=item from=$list name="myLoop" >% <% if $myLoop_index == 1 >%--<% $myLoop_index >%-- <% $item >% <% else >% <%if $myLoop_index == $myLoop_count >%++<% $myLoop_index >%++ <% $item >% <% else >% <% $myLoop_index >%) <% $item >% <% /if >% <% /if >% <% /foreach >% OUTPUT ============================= --1-- one 2) two 3) three 4) four 5) five 6) six 7) seven 8) eight ++9++ nine
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 %> 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
include
include
to begin with. Included files are also compiled and optionally saved for faster execution.
EXAMPLE ============================= <% include file="template.tpl" %> <% include file=$page %>
virtual
EXAMPLE ============================= <% virtual file="bloc.html" %> <% virtual file="/include/bloc.html" %> <% virtual file="bloc.html" path="/path/to/a/dir/from/rootdir" %> <% virtual file=$page %> <% virtual file=$page assign="my_var" %>
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
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
, gt
, lt
, lte
, le
, gte
, ge
, ==
, !=
, <
, >
, <=
, >=
. It is important to note that the comparison operators must be separated from the variables by at least one space.
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') . "
"; <% /php %>
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 %>