That's not really Smarty then now is it. It's some sort of delicious Low-Fat-Smarty you've cooked up. Kudos!AKA Panama Jack wrote:It can...
I should know since I developed a faster and smaller version of Smarty.
But I also have to remember that my pages still have to load the bloated Smarty class everytime, plus perform multiple stats to check if it needs to "recompile" (or "deprogram" as I like to say) my templates from smarty back to PHP. Using PHP as your template engine eliminates all that.AKA Panama Jack wrote: You have to remember that the template file is onlay parsed ONCE and converted into a PHP program that is called everytime afterwards without any need to parse the original.
I renamed the class to Webview and added some more variables to mimic Smarty's (just for timing).
Here's the class:
Code: Select all
<? // Try it yourself!
class Webview
{
public $template_dir = '';
public $compile_dir = '';
public $config_dir = '';
public $cache_dir = '';
private $_templateVars = array();
public function assign($name, $value)
{
if (!strlen($name)) return;
$this->_templateVars[$name] = $value;
}
public function display($_viewName)
{
foreach ($this->_templateVars as $_key => &$_val) {
$$_key = $_val;
}
$_errorSetting = ini_get('display_errors');
ini_set('display_errors', 0);
include $this->template_dir . $_viewName . '.tpl.php';
ini_set('display_errors', $_errorSetting);
}
}
?>
Code: Select all
<table>
<tr><th>Key</th><th>Value</th></tr>
<?foreach ($myArray as $key => $val):?>
<tr><td><?=$key?></td><td><?=$val?></td></tr>
<?endforeach;?>
</table>
Smarty: 0.0220789909363 sec (does not include the original parsing!)
Webview class: 0.00203585624695 sec
That's 10 times slower folks. What miracle is Smarty providing us that would justify my pages running 10 times slower (not always that much slower, but a lot of the times - and sometimes even slower)? I can't think of one. So, as a general rule, I don't use Smarty unless my boss forces me to. I just use a simple class similar to the one above.
Also, as an aside (and since many of us here seem to actually be interested in implementing an MVC-ish framework), you'll notice the Webview class stresses the use of named views, like
Code: Select all
$t->display('product_listing')Code: Select all
$t->display('product_listing.tpl.html')