Replace Smarty with PHP templates

Replace Smarty with PHP templates

In many performance guides, Smarty is considered to be removed to speed up things. But oftentimes it’s not Smarty causing performance problems, but rather big modifier chains not being cached. To point this out, we need to profile our template which is quite difficult when Smarty compiles in into something unreadable. So we need a quick and easy way to replace the Smarty template engine with pure PHP code. Since Smarty can’t do more than PHP, let’s replace Smarty with simple PHP templates.

So I’m providing here a small guide to replace Smarty with simple PHP based templates. These can be also cached by APC without any compiler.

First thing: Smarty configuration files
e.g. core.conf

foo = bar

[core]
logo = public/img/logo.png
link = http://www.simple-groupware.de
notice = Photo from xy
bg_grey = #F5F5F5

Now let’s convert it to PHP:
core_conf.php

  $config = array(
"logo" => "public/img/logo.png",
"bg_grey" => "#F5F5F5"
);
$config["core"] = array(
"logo" => "public/img/logocore.png",
"link" => "http://www.simple-groupware.de",
"notice" => "Photo from xy"
);


Next thing: Smarty templates
main.tpl

http://blog.mousavi.net/wp-content/uploads/2013/01/{config_load file="core.conf" section="core"}



http://blog.mousavi.net/wp-content/uploads/2013/01/{$smarty.const.TITLE|escape:"html"}: <br /> http://blog.mousavi.net/wp-content/uploads/2013/01/{$page.title|default:$page.name|escape:"html"}






    http://blog.mousavi.net/wp-content/uploads/2013/01/{foreach from=$list key=key item=item}
  • http://blog.mousavi.net/wp-content/uploads/2013/01/{$key}: http://blog.mousavi.net/wp-content/uploads/2013/01/{$item|escape:"html"}

  • http://blog.mousavi.net/wp-content/uploads/2013/01/{/foreach}



Conversion to PHP:
main_tpl.php

 


<?= q(TITLE) ?>: <br /> <?= q($this->page['title'] ?: $this->page['name']) ?>






    list as $key=>$item) http://blog.mousavi.net/wp-content/uploads/2013/01/{ ?>
  • :





You already noticed, we use “$this” in the template, so we need a class context to register variables and a small function to quote content called “q”. So let’s create a class that makes PHP templates as nice as Smarty templates:

Template.php

class Template http://blog.mousavi.net/wp-content/uploads/2013/01/{ 
function render($php) http://blog.mousavi.net/wp-content/uploads/2013/01/{
ob_start();
include($php);
return ob_get_clean();
}

function __get($unused) http://blog.mousavi.net/wp-content/uploads/2013/01/{
return '';
}

static function config($section, $key) http://blog.mousavi.net/wp-content/uploads/2013/01/{
static $config = null;
if ($config == null) include('core_conf.php');
return isset($config[$section]) ?
$config[$section][$key] : $config[$section];
}

function c($section, $key) http://blog.mousavi.net/wp-content/uploads/2013/01/{
return self::config($section, $key);
}
}

function q($str) http://blog.mousavi.net/wp-content/uploads/2013/01/{
return htmlspecialchars($str, ENT_QUOTES);
}

With this small class we can write:
index.php

require 'Template.php';
$tpl = new Template();
$tpl->list = array('item1', 'item2', 'item3');
$tpl->description = 'Hello World!';
echo $tpl->render('main_tpl.php');

And we’re done!