How do I choose a template engine?

Not so long ago, I came across the fact that I needed a template engine. I've heard of smarty, but I don't like its ideology. In my search, I found xtemplate thanks to an article about classification of template engines.

Thanks to her, I saw that there were many of them. Naturally, I had a question. Why are there so many of them? Surely there is some sense in the fact that there are many of them and they are different.

Therefore, I would like to know if there are any advantages to the different templates for specific situations. Or else only ideology plays a role here.

Author: Nicolas Chabanovsky, 2011-02-27

5 answers

I think this link will replace 1000 words. :)

The article is a comparative review of php template engines and its continuation.

 1
Author: xhr, 2011-03-11 08:28:47

An example of how you can use php as a template engine:

function template($file, $arr = array()) {
    foreach ($arr as $k=>$v) $$k = $v;
    ob_start();
    include $file;
    $data = ob_get_contents();
    ob_end_clean();
    return $data;
}

$data = template("/path/to/template.php", array("param1"=>"value1", "param2"=>"value2"));

print $data;

And no one forces you to write logic in template.php :)

 1
Author: Alex Silaev, 2011-02-28 08:59:12

Try Ptal http://www.ptal.ru/sintaksis And also, http://phptal.org/

To your question "how to choose a template engine?", there seems to be no clear answer. Everyone chooses their own. And this topic is very holivarnaya.

But if you start with the essence of the problem - "mixed PHP+HTML code is difficult to read", it turns out that the initial task of the template engine is to improve the syntax. So, when choosing a template engine, I am guided by this.

For a long time used Smarty. But then switched to Ptal. And now I only use it. Any template engine(this one too) has a big problem: sometimes it's impossible to write complex code. In such cases, you have to use PHP. You need to treat this with understanding and not try to do everything and everything on the template engine. Complex things will have to be written in PHP anyway.

Use the template engine in moderation, design your application correctly. And you will succeed.

I will speak in favor of TAL template engines. The essence of templates is to set HTML blocks in them with some parameters, with some logic (for example, to output data in a loop). So, the TAL architecture most organically fits into the HTML code.

Yes, sometimes you can write in pure PHP. but at least you get very clearly delineated blocks of PHP and HTML. And not a complete mess, as if you use only native templates.

 1
Author: Павел Владимиров, 2011-04-27 17:36:08

Use native templates. php is a template engine by itself

<div>
<b><?php echo $var; ?></b>
</div>

And smarty and others like it are, by and large, a compiler for the interpreter.

 0
Author: milast, 2011-03-26 09:10:03

Often you have to put a template engine for projects, but Smarty, although good, is too heavy. To do this, I once wrote myself a micro-template engine(it does not pretend to be the most powerful, unique and brilliant), but for small projects it is enough with a head.
The template engine class script itself

<?php
class Tpl_Class
    {
    var $val = array();
    var $Tpl;
    function tpl_get($tpl_name)
        {
        if(empty($tpl_name) OR !file_exists($tpl_name))
            {
            die('Error template');
            }
        else
            {
            $this->html = implode('', file($tpl_name));
            }
        }
    function set_value($key,$var)
        {
        $key = '{'.$key.'}';
        $this->val[$key] = $var;
        }
    function tpl_parsing()
        {
            foreach($this->val as $find => $replace)
                    {
                    $this->html = str_replace($find, $replace, $this->html);
                    }
        }
    }

In the php code itself, you write

<?php
DEFINE('DIR_T', 'template/');// определяем путь к папке с шаблонами
include_once DIR."tpl.class.php";// Подключаем класс с шаблонизатором

$tpl=new Tpl_Class;// Создаем новый класс
    $tpl->tpl_get(DIR_T.'index.tpl');//Подключаем шаблон
    $tpl->set_value('TITLE', 'Микро шаблонизатор'); // Title страницы
    $tpl->set_value('TITLE_PAGE', 'Шаблонизатор 21 Века'); // Загаловок страницы
    $tpl->set_value('COPY', '&copy; Peredero Ivan 2011');// Копирайт
    $tpl->tpl_parsing(); // Теперь обрабатываем заменяя переменные в шаблоне на наши данные
    echo $tpl->html;// Выводим обработанный шаблон на экран

Well, in the tpl template, it is clear that where the TITLE will be <title>{TITLE}</title> and so on

 0
Author: AHXAOC, 2011-04-19 12:22:20