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?

  • @Stanislav Dalinin, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

3 answers 3

@ Stanislav Dalinin , never use eval. This function is needed in very narrowly specialized cases when you need to simulate code execution (for example, dynamically creating classes, but here I am a supporter of the virtual file system - with proper autoloader, you can track the origin of the class directly from the code). All code that needs to run should connect as is. Everything else must be handled by existing code. Any template engine is a program that works with tokens in a text template , there is a conditional logic, but there is no directly native code there and should not be there. In other words, the template should look like this:

 <tag> {{ str }} </tag> 

And the template engine should already figure out that {{ str }} is a construct for outputting the str context.

  • I would not use the bike, but I would take all kinds of twig ... But in order to educate myself, the bike is not a bad thing - ArchDemon

There is another option (do not use eval )

 ob_start(); include ($this->templates_dir.$name.".tpl"); $str = ob_get_contents(); ob_end_clean(); echo $str; 

    and so?

    eval ("$ str = \" $ str \ ";");