3 good reasons not to use php template engines! Say NO to Smarty!

Friendly speaking I thought that I would never touch this topic,  but recently I had to apply changes and fixes to one project and was really surprised to see template engine that uses str_replace...

Resons for not using template engine

  1. PHP is a Hypertext Preprocessor and can be embedded into PHP. It is the main benefit of PHP. So, why one should refuse it?
  2. 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.
  3. 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);
		self::$Vars[$file] = array();
	static public function Get(){
		extract(self::$Vars['core'], EXTR_OVERWRITE | EXTR_REFS);
		extract(self::$Vars[$file], EXTR_OVERWRITE | EXTR_REFS);
		self::$Vars[$file] = array();
		return ob_get_clean();

Script code:


Tpl::Add("V1", "Hello", 'test');
Tpl::Add("V2", "World!", 'test');

And template:

	<title><?=$V1?> <?=$V2?></title>
	<?=$V1?> <?=$V2?>

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.


Posted by:
Enjoyed this post? Share and Leave a comment below, thanks! :)