Smarty is a template engine for PHP, and one that is very easy to use at that.
It can be downloaded from http://www.smarty.net/download.php or is included in some Linux distributions.
Rather than repeat them all here, there are easy to use installation instructions on the Smarty website at http://www.smarty.net/quick_start.php
There are two ways primary ways to assign variables to Smarty, directly as a variable, e.g. a string or an integer which can be down via the following:
//assign an integer of 1 to the variable 'var1'
$smarty->assign('var1',1);
//assign a string 'test' to the variable 'var2'
$smarty->assign('var2','test');
//assign an array to the variable 'var3'
$smarty->assign('var3',array(1,2,3,4));
You can also pass an associative array to assign multiple variables at once, e.g:
$smarty->assign(array('var1' => 1,'var2' => 'test', 'var3' => array (1,2,3,4)));
This will have the same effect as the 3 lines above.
In the Smarty template, to use the variables, all you would need to do is the following:
{$var1}, this will output the value of $var1 wherever you write this.
The second way is to assign by reference, you would use this if you are assigning an object to the template, for example given a class called HelloClass that returns "Hello World" when you call its hello() function, you can assign it to smarty with:
$smarty->assign_by_ref('hello_class',new HelloClass);
and in the template you would call it with {$hello_class->hello()}
To display a template, the code is as follows:
$smarty->display('template.tpl');
There is much much more to Smarty, you can find its full manual at http://www.smarty.net/manual/en/
Sunday, 24 August 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment