Hello.
I write my template engine.
class TemplateAlfa { private $templates_dir; private $templates_data; private $template; public function __construct($path){ $this->templates_dir = $path; } public function set($name, $value) { $this->templates_data[$name] = $value; $this->template .= file_get_contents($this->templates_dir.$name.".tpl"); } public function __get($name) { if (isset($this->templates_data[$name])) return $this->templates_data[$name]; else return ""; } public function wrap(){ } public function dysplay() { echo $this->template; } public function render() { $str = $this->template; eval("$str = "$str";"); echo $str; } }
There is a .tpl template with code:
<!DOCTYPE html> <html> <head> <title><?= $this->header['title']?></title> </head> <body>
and the file that calls it:
$template = new TemplateAlfa($root_dir."template/manager/"); $header = array( "title" => "Групповой редактор", "meta" => "", "link" => array( "http://fonts.googleapis.com/css?family=Roboto+Condensed:400,300,700&subset=latin,cyrillic-ext", "/template/manager/css/style.css", "/template/manager/css/modal.css" ), "scripts" => array( "/template/manager/javascript/common.js" => "defer", "/template/manager/javascript/dom.js" => "" ) ); $template->set("header", $header); $template->render();
The idea is:
Create an array with data for the template.
Call $template->set("Имя шаблона", $его_данные);
.
The set function saves data and reads the pattern, putting it into a variable. Thus, a full template ( $template
) and a full array with data ( $templates_data
) for different templates are gradually formed.
And the render();
function render();
should run php, which is stored in $template
, but this does not happen.
Question: why and how to live with it?