Archives for: December 2009, 23
Smarty resources - template from string
Smarty templates are usually stored in a template file, but in certain cases this may not be desirable. Another possibility for storing the templates is database and the documentation contains nice example . But, in a structured Php code, the code directly interacting with the database will be in another layer than the code using the template for displaing the results. Moreover, the template from the database should be loaded together with other related data, to make the database query more efficient. Bellow is an example of a display-layer class that uses templates already fetched from the database and together with other data stored in a data-layer classes. I was to some extend inspired by this smarty-related post. I apologise for possible errors, the example is derived from a working real code, but it is a simplified and not tested version.
/**
* Display class outputs all categories starting at certain node
*
* @uses Categories_Data_Categories
* @package Categories
* @subpackage Views
* @copyright WebDnes.cz
* @author Vaclav Mach
*/
class Categories_Views_List {
private $DataCategories;
private $smarty;
private static $mktime;
private static $template;
/**
* @param mixed $smarty ... an instance of Smarty object
*/
public function __construct($tree, $id_parent=0, $smarty) {
$this->DataCategories=new Categories_Data_Categories($tree, $id_parent);
$this->smarty=$smarty;
/**
* here we register static function of this display class as the smarty resources
* note that the functions are public to be accesible for smarty
*/
$smarty->register_resource("text", array("Categories_Views_List::text_get_template",
"Categories_Views_List::text_get_timestamp",
"Categories_Views_List::text_get_secure",
"Categories_Views_List::text_get_trusted"));
$this->template_article=$this->get_template_article($tree);
}
/**
* iterates the collection of data classes
* and sends them one by one to the display function
*/
public function DisplayAll() {
$ret="";
while ($DataCategory = $this->DataCategories->GetCategoryClass()) {
$ret.= $this->DisplayCategory($DataCategory);
}
return $ret;
}
/**
* this is the actual diplay function, important for this example
*
* @param Categories_Data_Category $DataCategory is a data class containing all the necessary data
* template, templatemodification time, and textual data e.g. category title and description are stored in the same table
*/
private function DisplayCategory(Categories_Data_Category $DataCategory) {
/**
* every piece of data, this means every data class may contain its own template
* the template is assigned to static variable to
* make it accessible for the registered resource functions
*/
self::$template=$DataCategory->Gettemplate();
self::$mktime=$DataCategory->GetLastModifiedTime();
// and this is the textual data for the template
$title=$DataCategory->GetTitle();
$description=$DataCategory->GetDescription();
$this->smarty->assign("cat_title", $DataCategory->GetTitle());
$this->smarty->assign("cat_description", $DataCategory->GetDescription());
// or shorter way
$this->smarty->assign("cat_img", $DataCategory->GetImage());
/**
* the name of the template is unique for each data class
* i do know smarty enough to be sure this is necessary for e.g. catching
* appending .tpl is just custom
*/
$templatename="cat".$DataCategory->GetId().".tpl";
/**
* in this call, smarty uses the static resources functions bellow and assigns
* variables to the template
* note that the template was specified just few lines above and is therefore specofic
* for the data in every data class in the DataCategories collection
*/
$ret= $this->smarty->fetch("text:$templatename");
return $ret;
}
public static function text_get_template($tpl_name, &$tpl_source, &$smarty)
{
/**
* uses the static variable assigned in the DisplayCategory function
*/
if(!empty(self::$template)) {
$tpl_source=self::$template;
return true;
}
return false;
}
public static function text_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty)
{
/**
* uses the static variable assigned in the DisplayCategory function
*/
if(!empty(self::$mktime)) {
$tpl_timestamp=self::$mktime;
return true;
}
return false;
}
public static function text_get_secure($tpl_name, &$smarty)
{
// assume all templates are secure
return true;
}
public static function text_get_trusted($tpl_name, &$smarty)
{
// not used for templates
}
}
?>
