Variables in Smarty


 

Variables in Smarty

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>

0

<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>

1

</table>

</font>

</body>

2

</html>

Smarty Arrays:

3

You can use smarty associative array variables as well as indexed arrays same as PHP.

Array.php

<?php

4

require './libs/Smarty.class.php';

$smarty=new Smarty;

5

$smarty->caching =true;

6

$smarty->assign('array1',array(1,2,3,4,5,67,8,99) );

$smarty->assign('array2',array('name'=>'Roseindia','city'=>'new delhi'));

7

$smarty->display('Array.tpl');

?>

8

Array.tpl

<html>

<head>

9

<title> Array </title>

</head>

<body bgcolor="#ffffff" >

0

<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>

1

</table>

</body>

</html>

2

 

Config file variables:

3

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

4

require './libs/Smarty.class.php';

$smarty=new Smarty;

5

$smarty->caching=true;

6

$smarty->display('confi.tpl');

?>

7

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>

8

<head>

<title>{#pagetitle#}</title>

</head>

9

<body bgcolor="{#bgcol#}">

<table cellspacing="{#cspace#}" style="{#tstyle#}" border="{#tbrdrsize#}" bgcolor="{#tbgcolor#}">

<tr><td>Hello</td></tr>

0

</table>

</body>

</html>

1

The second way to access the conf variable is $smarty.config method:

{config_load file='new.conf'}

2

<html>

<head>

<title>{$smarty.config.pagetitle}</title>

3

</head>

<body bgcolor="{$smarty.config.bgcol}">

<table cellspacing="{$smarty.config.cspace}" style="{$smarty.config.tstyle}" border="{$smarty.config.tbrdrsize}" bgcolor="{$smarty.config.tbgcolor}">

4

<tr><td>Hello</td></tr>

</table>

</body>

5

</html>

new .conf

{* This is a configuration file*}

6

{* In this file we assign different values to different variables *}

{* which will be used in in .php file *}

pagetitle="Configuration variable example"

7

bgcol="#ffffff"

tbrdrsize=3

tbgcolor="#a00fff"

8

tstyle="font-weight:bold;font-size:50"

cspace=30

9

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

Ads