Friday, November 23, 2007

Try Zend Framework vol.7 "Using Simplate as Template Enginge"

As you guys know "Smarty" is pretty famous template engine.
But, "Simplate" is likely faster than "Smarty".
And the way of writing template is almost same as smarty.
So I tried using it as zend view template.

First, you need to install simplate as php extension, but don't worry it's pretty easy.
All you need to do is bellow.
* In my case installed php at /usr/local/php525, and make simbolick link like below.
php5 -> /usr/local/php525/bin/php
phpize5 -> /usr/local/php525/bin/phpize.

Ok, install simplate extension.
# cd /usr/local/src
# wget http://simplate.aimy.jp/archive/simplate-0.3.7.tar.gz
# tar zxvf simplate-0.3.7.tar.gz
# cd simplate-0.3.7
# phpize5
# ./configure \
--enable-simplate \
--with-php-config=/usr/local/php525/bin/php-config \
--prefix=/usr/local/php525 \
# make
# make install


If you see the message like below, you would success to install simplate extension.
Installing shared extensions:     /usr/local/php525/lib/php/extensions/no-debug-non-zts-20060613/


Next, you need to modify php.ini.
# vim /usr/local/php525/lib/php.ini

Add this line.
extension=simplate.so

Then reload apache. Don't forget to check if the extension is installed like this.
php5 -i | grep simplate


You should see message like this.
simplatesimplate support => enabled


Ok, we are ready to make simplate view!

library/Hoge/View/Simplate.php
<?php
require_once 'Zend/View/Interface.php';

class Hoge_View_Simplate implements Zend_View_Interface
{
/**
* Simplate object
* @var Simplate
*/
protected $_simplate;

/**
* constructor
*
* @param string $tmplPath
* @param array $extraParams
* @return void
*/
public function __construct($tmplPath = null, $extraParams = array())
{
$this->_simplate = new Simplate;
if (null !== $tmplPath) {
$this->setScriptPath($tmplPath);
}
foreach ($extraParams as $key => $value) {
$this->_simplate->$key = $value;
}
}

/**
* Return template enginge object.
*
* @return Simplate
*/
public function getEngine()
{
return $this->_simplate;
}

/**
* Set template's path
*
* @param strimg $path Directory path
* @return void
public function setScriptPath($path)
{
if (is_readable($path)) {
$this->_simplate->template_dir = $path;
return;
}
throw new Exception("You specified unrecognized path '{$path}'");
}

/**
* Get current template path.
* @return string
*/
public function getScriptPaths()
{
return $this->_simplate->template_dir;
}

/**
* Alias of setScript path.
*
* @param string $path
* @param string $prefix Unused
* @return void
*/
public function setBasePath($path, $prefix = 'Zend_View')
{
return $this->setScriptPath($path);
}

/**
* Alias of setScript path.
*
* @param string $path
* @param string $prefix unused
*
* @return void
*/
public function addBasePath($path, $prefix = 'Zend_View')
{
return $this->setScriptPath($path);
}

/**
* Set valiables to template.
*
* @param string $key valiable name
* @param mixed $val valiable value
* @return void
*/
public function __set($key, $value)
{
$this->_simplate->assign($key, $value);
}

/**
* Get value.
*
* @param string $key valiable name
* @return mixed valiable value
*/
public function __get($key)
{
return $this->_simplate->_tpl_vars[$key];
}

/**
* Enable test for empty() isset().
*
* @param string $key
* @return boolen
*/
public function __isset($key)
{
return isset($this->_simplate->_tpl_vars[$key]);
}

/**
* Enable unset to object valiable.
* @return void
*/
public function __unset($key)
{
unset($this->_simplate->_tpl_vars[$key]);
}

/**
* Set valiable to template.
* set value to specific key.
* set at once by key => value array.
*
* @see __set()
* @param string|array $spec type of settin value (key or key => value array)
* @param mixed $value (optionale) specify the name of key. set here.
* @return void
*/
public function assign($spec, $value = null)
{
if (is_array($spec)) {
foreach($spec as $key => $value) {
$this->_simplate->assign($key, $value);
}
return;
}
$this->_simplate->assign($spec, $value);
}

/**
* Clear all valiables.
* Clear all valiables sat to Zend_View by {@link assign()} or {@link __get()}/{@link __set()}.
*
* @return void
*/
public function clearVars()
{
$this->_simplate->_tpl_vars = null;
}

/**
* Dispatch template and return result.
*
* @param string $name template name
* @return string result
*/
public function render($name)
{
return $this->_simplate->fetch($name);
}
}


Make method for displaying simplate template with assigning 20000 variables.
* Don't forget writing $this->_helper->viewRenderer->setNoRender();
otherwise zf automatically expect the template which has same name as method and has phtml suffix then render it, even if you specify another desired template.

application/controllers/HogeController.php
function simplateAction()
{
$this->_helper->viewRenderer->setNoRender();
require_once 'Hoge/View/Simplate.php';
$tplPath = dirname(dirname(__FILE__)) . '/views/scripts/hoge/';
$params = array(
'compile_dir' => dirname(dirname(__FILE__)) . '/views/templates_c',
'left_delimiter' => '{',
'right_delimiter' => '}',
);
$view = new Hoge_View_Simplate($tplPath, $params);
foreach(range(0, 20000) as $i) {
$vars[$i] = "Hello World $i";
}
$view->vars = $vars;
echo $view->render('simplate.tpl');
}


This is template.
<html>
<head></head>
<body>
{foreach key=key item=item from=$vars}
{$key} : {$item} <br />
{/foreach}
</body>
</html>


That's it. You will see 20000 Hello Worlds!

But, it's not enough, because I didn't check if simplate is faster than smaty template.
So, I compare "Smaty" with "Simplate".

Get time before execution.

application/controllers/HogeController.php
function preDispatch()
{
$this->startTime = microtime(true);
}

function postDispatch()
{
echo (microtime(true) - $this->startTime);
}


For Smaty template view you should see here

Controllers' method is this. Almost same as simplate.
application/controllers/HogeController.php
function smartyAction()
{
$this->_helper->viewRenderer->setNoRender();
require_once 'Hoge/View/Smarty.php';
$tplPath = dirname(dirname(__FILE__)) . '/views/scripts/hoge/';
$params = array(
'compile_dir' => dirname(dirname(__FILE__)) . '/views/templates_c',
);
$view = new Hoge_View_Smarty($tplPath, $params);
foreach(range(0, 20000) as $i) {
$vars[$i] = "Hello World $i";
}
$view->vars = $vars;
echo $view->render('smarty.tpl');
}


Template is same as simplate.

Here the result of rendering time.
Simplate: 0.137821912766
Smarty : 0.153032064438


Faster than smaty. :-)
(weight 89.4kg BMI 33%)

Labels:

0 Comments:

Post a Comment

<< Home