Variables in Smarty - Types, Examples etc.
Variables in Smarty - Types, Examples etc.Variables in Smarty Templates Engine for PHP
Smarty Template variables are much like PHP variable, they can contain numbers, letters, and underscores with a preceding dollar ($) sign.
We can reference
Smarty Arrays that are indexed either numerically or non-numerically.
Object properties and methods,
Config file variables
Smarty Variables can be declared as:
{$new}, {$1212},{$_}
Except this naming convention other special symbols are not valid.
Example of Smarty Variable Types:
Var.php
<?php
require './libs/Smarty.class.php';
$smarty=new Smarty;
$smarty->caching =true;
$smarty->assign('name','roseindia' );
$smarty->assign('address' , ' new delhi');
$smarty->display('Var.tpl');
?>
Var.tpl
<html> <head><title> Simple variable</title></head> <body bgcolor="#ffffff"> <font size=2> <table cellspacing=30 style="font-weight:bold;font-size:50"> <tr><td >Name:</td><td>{$name}</td></tr> <tr><td>Address:</td><td>{$address}</td></tr> </table> </font> </body> </html>
Smarty Arrays:
You can use smarty associative array variables as well as indexed arrays same as PHP.
Array.php
<?php
require './libs/Smarty.class.php';
$smarty=new Smarty;
$smarty->caching =true;
$smarty->assign('array1',array(1,2,3,4,5,67,8,99) );
$smarty->assign('array2',array('name'=>'Roseindia','city'=>'new delhi'));
$smarty->display('Array.tpl');
?>
Array.tpl
<html> <head> <title> Array </title> </head> <body bgcolor="#ffffff" > <table cellspacing=30 style="font-weight:bold;font-size:50"> <tr><td>First Example:</td><td>$array1[2]</td><td>{$array1[2]}<!--Display third element of the array--></td></tr> <tr><td>Second Example:</td><td>$array2.name</td><td>{$array2.name}<!-- Using associative array--></td></tr> </table> </body> </html>
Config file variables:
To use config file variables we need three files: .php file, .tpl file, & .conf file which stores in (C:\wamp\www\smarty\configs)
confi.php file
<?php
require './libs/Smarty.class.php';
$smarty=new Smarty;
$smarty->caching=true;
$smarty->display('confi.tpl');
?>
confi .tpl file
( We can access the conf variable from .conf file by putting the variable inbetween the '#' sign {##}: )
{config_load file='new.conf'} <html> <head> <title>{#pagetitle#}</title> </head> <body bgcolor="{#bgcol#}"> <table cellspacing="{#cspace#}" style="{#tstyle#}" border="{#tbrdrsize#}" bgcolor="{#tbgcolor#}"> <tr><td>Hello</td></tr> </table> </body> </html>
The second way to access the conf variable is $smarty.config method:
{config_load file='new.conf'} <html> <head> <title>{$smarty.config.pagetitle}</title> </head> <body bgcolor="{$smarty.config.bgcol}"> <table cellspacing="{$smarty.config.cspace}" style="{$smarty.config.tstyle}" border="{$smarty.config.tbrdrsize}" bgcolor="{$smarty.config.tbgcolor}"> <tr><td>Hello</td></tr> </table> </body> </html>
new .conf
{* This is a configuration file*} {* In this file we assign different values to different variables *} {* which will be used in in .php file *} pagetitle="Configuration variable example" bgcol="#ffffff" tbrdrsize=3 tbgcolor="#a00fff" tstyle="font-weight:bold;font-size:50" cspace=30
In the above new.conf file we need to assign our desired value to particular variables, which will be used in the .tpl file.
Generally above mentioned variables are attributes of HTML