 |
Flat-Frog - the faster compiling PHP template engine |
Functions
Description
This function will create a new variable from the template and assign a value to it.
Arguments
- name
The name of the variable that you are assigning a value to.
- value
The value of the variable. This can be a string literal, or another variable, modified perhaps. It can also be a concatenated variable.
Example
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.
Description
This function will load a config file into a template.
Arguments
- file
The name of the config file to load.
- section (optional)
The section to load.
- var (optional)
The variable to load.
Example
EXAMPLE
=============================
<% config_load file="config.conf" %>
<% #variable# %>
Description
Much as the name describes, this function behaves almost exactly like foreach. It will loop through an array and return the output.
Arguments
- from
The array we are going to loop through.
- value
The name of the variable that will hold the current variable value.
- key (optional)
The name of the variable that will hold the current key.
- name (optionnal)
The name of the loop.Used to create two variables :
<name>_index : The index of the loop starting at 1.
<name>_count :The number of element of the loop.
Example if the name of the variable is myLoop Flat-Frog creates $myLoop_index and $myLoop_count
Example
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
Description
This function will loop through a block the specific number of times. It is very similar to PHP's for. You can specify a starting integer, an ending integer, and a step value.
Arguments
- start
An integer indicating the starting point of our loop.
- stop
An integer indicating the ending point of our loop.
- step
An integer indicating how many integers we are going to jump with each interation.
- value (optional)
What local variable we want to store the current position in, if anyway.
Example
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
Description
This function will process another template and include the results in the original template. Alternatively, it will process another template and store the results in a variable for use in the template that called the include to begin with. Included files are also compiled and optionally saved for faster execution.
Arguments
- file
The file we are going to include.
- assign (optional)
This will optionally assign the included file to a variable instead of outputting it into the current template body.
Example
EXAMPLE
=============================
<% include file="template.tpl" %>
<% include file=$page %>
Description
This function will insert the content of a file as is. It differs from include as the files are not interpreted by Flat-Frog nor cached in anyway. The file is by default looked in a sub path of the DOCUMENT_ROOT. You can provide an optionnal argument which is an absolute path. Warning : when using php in CLI mode, the variable $_SERVER['DOCUMENT_ROOT'] is empty. You should use the attribute path in that case.
Arguments
- file
The file we are going to include.
- path
The path from the root of the system.
- assign (optional)
This will optionally assign the included file to a variable instead of outputting it into the current template body.
Example
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" %>
Description
This function will insert text into a template. The text is generated by calling a matching function created by the programmer in PHP that will create necessary text, such as returning compiled template information, or other information for inclusion in the calling template. The output of an insert is not cached and thus an insert is called everytime a template is loaded.
Arguments
- name
This is the name of the insert. Used to define what function to call. Read more in the example.
- additional args (optional)
This function will take an abitrary number of arguments and pass them along to the user-defined function.
Example
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
Description
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, 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.
Example
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%>
Description
These will insert the user-defined left delimiter and right delimiter into the template.
Example
TEMPLATE
=============================
Here is an example: <% ldelim %> config_load file="config.conf" <% rdelim %>
OUTPUT
=============================
Here is an example: <% config_load file="config.conf" %>
Description
This block function will take everything inside it and interpret it literally. This means that you can put template code inside of this function and it will not be parsed.
Example
EXAMPLE
=============================
<% literal %>
Here is an example: <% config_load file="config.conf" %>
<% /literal %>
Description
This block function will take everything inside it and execute it as pure PHP code. Everything in this block gets sent directly to the PHP compiler. To access variables from the template engine from inside a php block, use $this->get_vars(varname).
Example
EXAMPLE
=============================
<% php %>
echo "Hello to " . $this->get_vars('username') . "
";
<% /php %>
Description
Much like the name indicates, this function behaves very similar to PHP's switch function. It will execute a different block of code depending on the given value.
Arguments for switch
- from
This is the variable that we will switch on. It is what will be compared.
Arguments for case
- value
This is the value that we will compare to the original variable. If value isn't provided, this will be the default case. There can only be one default case per switch.
Example
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 %>
Home