Resons for not using template engine
- PHP is a Hypertext Preprocessor and can be embedded into PHP. It is the main benefit of PHP. So, why one should refuse it?
- Speed! Really, even when you are using "cool template engines" (no names here) with caching they will be much slower than plain PHP. Especially if you are enabling Eacelerator & optimize your code.
- New language really? Each template engine uses its own language at least for iterations, so one should learn it. What is the reason? is it more functional than PHP?
Problems & solutions
I understand that design should be separated from the program logic - just do not put the logic into the template! I understand that developers want to have one main template with placeholders for modules & templates for modules. PHP can handle this too, and I am sure you know it.
Just some code for illustration. Very basic template handling class:
class Tpl{ static public $Vars = array(); static public function Add($var, $val, $context){ self::$Vars[$context][$var] = $val; } static public function Out($file){ extract(self::$Vars['core'], EXTR_OVERWRITE | EXTR_REFS); extract(self::$Vars[$file], EXTR_OVERWRITE | EXTR_REFS); require("./templates/{$file}.html"); self::$Vars[$file] = array(); } static public function Get(){ extract(self::$Vars['core'], EXTR_OVERWRITE | EXTR_REFS); extract(self::$Vars[$file], EXTR_OVERWRITE | EXTR_REFS); ob_start(); require("./templates/{$file}.html"); self::$Vars[$file] = array(); return ob_get_clean(); } }
Script code:
include("class.Tpl.php"); Tpl::Add("V1", "Hello", 'test'); Tpl::Add("V2", "World!", 'test'); Tpl::Out("test");
And template:
<html>
<head>
<title><?=$V1?> <?=$V2?></title>
</head>
<body>
Working<br/>
<?=$V1?> <?=$V2?>
</body>
</html>
Sure, in real projects I am using more complicated solution with HTML cache, controls and validators, but is is using PHP as a template language.