Built - in Functions :
Smarty comes with several built-in functions. Built-in functions are integral to the template language. You cannot create custom functions with the same names, nor can you modify built-in functions.
Some of the built-in functions are given below.
Important :
The functions having '*' mark are not work in smarty of version 2
include
Include tags are used for including other templates in the current template.
You can also pass other attributes in include file.
Setting the optional assign attribute specifies the template variable that the output of {include} is assigned to, instead of being displayed.
Example:
{include file="indroduction.tpl.html"}
<div style="font-size: 18;color: black;"> Assign to variable</div><br/>
{include file='indroduction.tpl.html' assign=navbar}
{$navbar}
Demo:
capture
Capture is used to collect the output of the template into a variable instead of displaying it. Any content between {capture name="capture_name"} and {/capture} is collected into the variable specified in the name attribute. The captured content can be used in the template from the special variable $smarty.capture.capture_name where capture_name is the value passed in the name attribute. If you do not supply a name attribute, then "default" will be used. All {capture} commands must be paired with {/capture}. You can nest capture commands.
The assign attribute can be used to specify the variable name where to assign the captured output.
Example:
{capture name=banner assign=var_assign}
{include file='get_banner.tpl.html'}
{/capture}
{if $smarty.capture.banner }
The value after capture = {$smarty.capture.banner}<:br/>:
The value after capture for assign attribute= {$var_assign}
{/if}
Demo:
math
{math} allows the template designer to do math equations in the template.
Example:
{math assign="sum" equation="(X+Y)" X=5 Y=1}
{$sum}
if, elseif, else
{if} statements in Smarty have much the same flexibility as PHP if statements. Every {if} must be paired with an {/if}. {else} and {elseif} are also permitted.
Some of the recognized qualifiers are given below : -
Qualifier | Meaning |
= = | Equals |
!= | Not equals |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
! | Negation |
% | Modulus |
Example:
{if $fname=="Sunil"}
Welcome Sir.
{elseif $fname=="Ruby"}
Welcome Madam.
{else}
Sorry, you are not allowed.
{/if}
Demo:
literal
Literal tags allow a block of data to be taken literally. This is typically used around javascript or Cascading Style Sheet (CSS) blocks where curly braces would interfere with the template delimiter syntax. Anything within {literal} {/literal} tags is not interpreted, but displayed.
Example:
DEMO:
Example:-for the CSS
Demo:
function*
{function} is used to create functions within a template and call them just like a plugin function.
The {call} tag must be used to call a template function defined externally from the given template.Otherwise you can directly use the function as {funcname ...} in the template.
Example:
{function name=add data1=0 data2=0}
{$sum=$data1+$data2}{$sum}
{/function}
{add data1=15 data2=10}
//Directly call to function
{call name=add data1=10 data2=10} //By {call} tag call to function
while*
{while} loops in Smarty have much the same flexibility as PHP while statements,
with a few added features for the template engine. Every {while} must be paired with a matching {/
while}. All PHP conditionals and functions are recognized, such as ||, or, &&, and, is_array(), etc.
Example:
{$x=1}
{while $x lt 10}{$x++}{/while}
Second while loop
{while $x lt 20}{$x}{$x=$x+3}{/while}
for, forelse*
The {for}{forelse} tag is used to create simple loops.
{for $var=$start to $end} simple loop with step size of 1.
{for $var=$start to $end step $step} loop with individual step size.
{forelse} is executed when the loop is not iterated.
Example:
{for $foo=1 to 3 }
{$foo}
{/for}
{for $foo=1 to 10 step 2}
{$foo}
{/for}
{for $foo=1 to 10 max=5}
{$foo}
{/for}
foreach, foreachelse
foreach loops are an alternative to section loops. Foreach is used to loop over a single associative array. The syntax for foreach is much easier than section, but as a tradeoff it can only be used for a single array. Foreach tags must be paired with /foreach tags. Required parameters are 'from' and 'item'.
Example:
{foreach from=$details item=detail name=array_index}
Name: {$detail}
Index={$smarty.foreach.array_index.index}
{if $smarty.foreach.array_index.first}
First itration={$smarty.foreach.array_index.first}
{/if}
{if $smarty.foreach.array_index.last}
Last itration={$smarty.foreach.array_index.last}
Total={$smarty.foreach.array_index.total}
{/if}
Iteration={$smarty.foreach.array_index.iteration}
{if $smarty.foreach.array_index.show} show= {$smarty.foreach.array_index.show}{/if}
<br/>
{foreachelse}
Record not found !!
{/foreach}
foreach key
Example:
In php side:-
$smarty->assign("contacts", array(array("Phone" => "1", "Fax" => "2", "Cell" => "3"),
array("Phone" => "555-4444", "Fax" => "555-3333", "Cell" => "760-1234")));
In template side:-
{foreach name=outer item=contact from=$contacts}
{foreach key=key item=item from=$contact}
{$key}: {$item}
{/foreach}
{/foreach}
section, sectionelse
Template sections are used for looping over arrays of data. All section tags must be paired with {/section} tags. Required parameters are 'name' and 'loop'.
The name of the section can be anything you like, made up of letters, numbers and underscores. Sections can be nested, and the nested section names must be unique from each other.
Example:
<table width="200" border="1" align="center">
<tr>
<td>ID</td>
<td>Name</td>
<td>Address</td>
</tr>
{section name=smarty_demo loop=$res}
<tr>
<td>{$res[smarty_demo].id}</td>
<td>{$res[smarty_demo].Name}</td>
<td>{$res[smarty_demo].Address}</td>
<td>{$smarty.section.smarty_demo.index}</td>
</td>{$smarty.section.smarty_demo.iteration}</td>
</td>{$smarty.section.smarty_demo.loop}</td>
{if $smarty.section.smarty_demo.first}
</td>first={$smarty.section.smarty_demo.first}</td>
{/if}
{if $smarty.section.smarty_demo.last}
</td>last={$smarty.section.smarty_demo.last}
total={$smarty.section.smarty_demo.total}
</td>
{/if}
{if $smarty.section.smarty_demo.show}
</td>show={$smarty.section.smarty_demo.show}</td>
{/if}
</tr>
{sectionelse}
No data found !!
{/section}
</table>
NOTE:We can use infinity numbers of sections i.e. nested section.
Difference between section and foreach
1.For SECTION you can start from a specific value, and can also set a step for the iteration, whereas for FOREACH you have to loop over all values.
2.Foreach is used for associative arrays.
- Suppose I have two files include_parent.tpl.html and include_child.tpl.html
Write down the code to include the file only once so that when I am trying to include the file more than once it will show me message.
- Find the value of the expression (b-(a+c*(e/d))) in smarty. Where
a=1,b=2,c=3,d=4,e=8.
- What will be the output:
x=4
y=8
y=y/x
if(y==2){
z=(x+y)*(y/z)
}
Do it in smarty.
- I have div in a html file and i have passed the value of variable $country as India
Write the javascript code so that i have the output as India is a beautiful country by using the $country variable.
- I have an array as
$a=array('a'=>array(a1=>array(a11,a12,a13)),'b'=>array(b1=>array(b11,b12,b13)),'c'=>array(c1=>array(c11,c12,c13)));
Write the code in smarty so that I have the output as
a11
|
a12
|
a13
|
b11
|
b12
|
b13
|
c11
|
c12
|
c13
|
- I have an array as
$a=array(0=>array('name'=>'lalit','address'=>'puri','age'=>24),1=>array('name'='amar','address'=>'tata','age'=>24));
Write the code in smarty so that I have the output as
lalit
|
puri
|
24
|
amar
|
tata
|
24
|