Professional Documents
Culture Documents
Smarty
Smarty
Assign
Description :This function will create a new variable from the template and assign a value to it.
Arguments
var
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
OUTPUT
config_load
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# }
foreach/foreachelse
Description :Much as the name describes, this function behaves 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.
Example
PHP
$tpl->assign("contacts", array(
array("phone" => "1", "fax" => "2", "cell" => "3"),
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")
));
TEMPLATE
OUTPUT
phone: 1<br>
fax: 2<br>
cell: 3<br>
phone: 555-4444<br>
fax: 555-3333<br>
cell: 760-1234<br>
for
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
A value indicating how much 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
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
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. You can also use an absolute path name to include
template files outside the defined template directory path.
Arguments
file
The file we are going to include. If you are using an absolute path it is recommended you place file: before the path.
assign (optional)
This will optionally assign the included file to a variable instead of outputting it into the current template body.
[var ...] (optional)
You can also pass variables to included templates as attributes. Any variables explicitly passed to an included template as
attributes are only available within the scope of the included file. Attribute variables override current template variables, in
the case they are named alike.
Example
{ include file="template.tpl" }
{ include file=$page }
{ include file="/usr/local/include/templates/header.tpl" }
{ include file="file:/usr/local/include/templates/header.tpl" }
insert
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
TEMPLATE
{ insert name="stuffandjunk" }
{ insert name="othercrap" var="hi" }
OUTPUT
if/elseif/else
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, 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.
NEW! The is token is now supported. The following expressions can be used. Expressions in brackets [] are optional. is [not] div by
is [not] even [by]
is [not] odd [by]
{* same as above *}
{if $name == "Fred" || $name == "Wilma"}
...
{/if}
ldelim/rdelim
Description : These will insert the user-defined left delimiter and right delimiter into the template.
Example
TEMPLATE
OUTPUT
literal
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 }
Php
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') . "<BR>";
{ /php }
section/sectionelse
Description :This function will loop through a block a specific number of times similar to the foreach function. All {section}
tags must be paired with {/section} tags. Required parameters are name and loop. A {sectionelse} is executed when there are
no values in the loop variable.
Arguments for section
name
Section name.
loop
An array of data used to determin the number of loops.
start (optional)
The index position that the section will begin looping. If the value is negative, the start position is calculated from the end
of the array. For example, if there are seven values in the loop array and start is -2, the start index is 5. Invalid values
(values outside of the length of the loop array) are automatically truncated to the closest valid value. Default = 0
step (optional)
The step value that will be used to traverse the loop array. For example, step=2 will loop on index 0,2,4, etc. If step is
negative, it will step through the array backwards. Default = 1
max (optional)
Sets the maximum number of times the section will loop.
show (optional)
This determines whether or not to show this section. Default = true
Properties for section
index
Display the current loop index.
Example: {$templatelite.section.customer.index}
index_prev
Display the previous loop index.
Example: {$templatelite.section.customer.index_prev}
index_next
Display the next loop index.
Example: {$templatelite.section.customer.index_next}
iteration
Display the current loop iteration.
Example: {$templatelite.section.customer.iteration}
first
True if first iteration, false for anything else.
Example: {$templatelite.section.customer.first}
last
True if lasy iteration, false for anything else.
Example: {$templatelite.section.customer.last}
rownum
Same as iteration property.
Example: {$templatelite.section.customer.rownum}
loop
Display the last index number that this section looped.
Example: {$templatelite.section.customer.loop}
show
Set to true or false based upon the show argument in the section tag.
Example: {$templatelite.section.customer.show}
total
Display the number of iterations that this section will loop.
Example: {$templatelite.section.customer.total}
Example
PHP
$player_id = array(1,2,3);
$tpl->assign('player_id',$player_id);
TEMPLATE
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
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 }
while
Description: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.
Arguments
expression
The expression to be evaluated.
Example
TEMPLATE
OUTPUT
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]