advantage for programmers

Smarty for Programmers

The advantage for programmers is that they write the code in a PHP file without having to combine the instructions with HTML. Also, if the designer changes the layout of a page the programmer doesn’t have to change the code to suit the new layout since the functionalities won’t change. You perform your work in your files, assign to the templates all the values needed to print on the site and go out for a beer.

The PHP file you need to include the Smarty class require 'Smarty.class.php'. After that you instantiate the smarty with $smarty = new Smarty.

To assign a variable to the template you need to $smarty->assign('UserName', 'Jeff Adams'). After everything is finished you call the method and to display the template $smarty->display('index.tpl').

A sample code like this (index.php) :

[php]
<code><span style="color: #0000bb; font-size: 10;"><php </span>
<span style="color: #007700; font-size: 10;">require </span><span style="color: #dd0000; font-size: 10;">’Smarty.class.php'</span><span style="color: #007700; font-size: 10;">; </span></code><span>
</span><code><span style="color: #0000bb; font-size: 10;">$smarty </span><span style="color: #007700; font-size: 10;">= new </span><span style="color: #0000bb; font-size: 10;">Smarty</span><span style="color: #007700; font-size: 10;">; </span></code><span>
</span><code><span style="color: #0000bb; font-size: 10;">$smarty</span><span style="color: #007700; font-size: 10;">-></span><span style="color: #0000bb; font-size: 10;">assign</span><span style="color: #007700; font-size: 10;">(</span><span style="color: #dd0000; font-size: 10;">’Username'</span><span style="color: #007700; font-size: 10;">, </span><span style="color: #ff8000; font-size: 10;">’Jeff Adams’); </span></code><span>
</span><code><span style="color: #0000bb; font-size: 10;">$smarty</span><span style="color: #007700; font-size: 10;">-></span><span style="color: #0000bb; font-size: 10;">display</span><span style="color: #007700; font-size: 10;">(</span><span style="color: #dd0000; font-size: 10;">’index.tpl'</span><span style="color: #007700; font-size: 10;">); </span></code><span>
</span><code><span style="color: #0000bb; font-size: 10;">?> </span></code>

[/php]


The template (index.tpl) like this:

[php]
<code><span style="color: #007700; font-size: 10;"><</span><span style="color: #0000bb; font-size: 10;">html</span><span style="color: #007700; font-size: 10;">> </span></code><span>
</span><code><span style="color: #007700; font-size: 10;"><</span></code><code><span style="color: #0000bb; font-size: 10;">body</span><span style="color: #007700; font-size: 10;">> </span></code><span>
<code> </code></span><code><span style="color: #0000bb; font-size: 10;">Welcome </span><span style="color: #007700; font-size: 10;">{</span><span style="color: #0000bb; font-size: 10;">$Username</span><span style="color: #007700; font-size: 10;">} </span></code><span>
<code><!–</code–></code></span><code><span style="color: #007700; font-size: 10;"><</span></code><code><span style="color: #0000bb; font-size: 10;">body</span><span style="color: #007700; font-size: 10;">> </span></code><span>
<code><!–</code–></code></span><code><span style="color: #007700; font-size: 10;"><!–</span–></span></code><code><span style="color: #007700; font-size: 10;"></</span></code><code><span style="color: #0000bb; font-size: 10;">html</span><span style="color: #007700; font-size: 10;">></span></code>

[/php]

Also create an array in PHP an pass it to the template:

[php]
<code><span style="color: #0000bb; font-size: 10;">$tmplate </span><span style="color: #007700; font-size: 10;">= array ( </span><span style="color: #ff8000; font-size: 10;">’UID’=> ’10’, &’Name’ => ‘Jeff Adams’, ‘Address’=>’Home address’); </span></code><span>
</span><code><span style="color: #0000bb; font-size: 10;">$smarty</span><span style="color: #007700; font-size: 10;">-></span><span style="color: #0000bb; font-size: 10;">assign</span><span style="color: #007700; font-size: 10;">(</span><span style="color: #ff8000; font-size: 10;">’info’, $tmplate);</span></code>

[/php]

Tags:

Request a Free SEO Quote