INTEGRATING SMARTY WITH THE ZEND FRAMEWORK

2016/06/20 19:13
阅读数 28
Inspired by this article I started to play around a bit to integrate the Smarty template engine into the Zend Framework. My ambition was to minimize the required code in the controller actions but stay close to the given Zend_View API. I also wanted to integrate the Smarty caching feature. Here is the code I came up with.

Class location

The class file is named by my company (Travello) and is placed in the Travello directory beneath the Zend Framework library include path. Its advantage is that the class will be auto loaded in my setup.

/Zend
/View
/Travello
/View
Smarty.php

Class definition and constructor

Let's start with the class definition and the constructor part. My class Travello_View_Smarty is extending the Zend_View_Abstract class. In the constructor the parent constructor from Zend_View_Abstract is called first. After that a Smarty object is instantiated, configured and stored in a private attribute.

Please note that I use a configuration object from the object store to get the configuration data for Smarty.

class Travello_View_Smarty extends Zend_View_Abstract
{
private Inspired by this article I started to play around a bit to integrate the Smarty template engine into the Zend Framework. My ambition was to minimize the required code in the controller actions but stay close to the given Zend_View API. I also wanted to integrate the Smarty caching feature. Here is the code I came up with.

Class location

The class file is named by my company (Travello) and is placed in the Travello directory beneath the Zend Framework library include path. Its advantage is that the class will be auto loaded in my setup.

  /Zend
/View
/Travello
/View
Smarty.php

Class definition and constructor

Let's start with the class definition and the constructor part. My class Travello_View_Smarty is extending the Zend_View_Abstract class. In the constructor the parent constructor from Zend_View_Abstract is called first. After that a Smarty object is instantiated, configured and stored in a private attribute.

Please note that I use a configuration object from the object store to get the configuration data for Smarty.

___FCKpd___1

Implement _run() method

The method _run() is the only method that needs to be implemented in any subclass of Zend_View_Abstract. It is called automatically within the render() method. My implementation just uses the display() method from Smarty to generate and output the template.

    protected function _run($template)
{
$this->_smarty->display($template);
}

Overwrite assign() method

The next part is an overwrite of the assign() method from Zend_View_Abstract, which works in a similar way. The big difference is that the values are assigned to the Smarty object and not to the $this->_vars variables array of Zend_View_Abstract.

    public function assign($var)
{
if (is_string($var))
{
$value = @func_get_arg(1);

$this->_smarty->assign($var, $value);
}
elseif (is_array($var))
{
foreach ($var as $key => $value)
{
$this->_smarty->assign($key, $value);
}
}
else
{
throw new Zend_View_Exception('assign() expects a string or array, got '.gettype($var));
}
}

Overwrite escape() method

The next part is an overwrite of the escape() method from Zend_View_Abstract. It works both for string and array values and also uses the escape() method from the Zend_View_Abstract. The advantage of this is that I don't have to care about each value of an array to get properly escaped.

    public function escape($var)
{
if (is_string($var))
{
return parent::escape($var);
}
elseif (is_array($var))
{
foreach ($var as $key => $val)
{
$var[$key] = $this->escape($val);
}

return $var;
}
else
{
return $var;
}
}

Print the output

The next method output() is a wrapper on the render() method from Zend_View_Abstract. It just sets some headers before printing the output.

    public function output($name)
{
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Cache-Control: post-check=0, pre-check=0", FALSE);

print parent::render($name);
}

Use Smarty caching

The last two methods were created to simply integrate the Smarty caching mechanism in the View class. With the first one you can check for cached template and with the second one you can set the caching on or of.

    public function isCached($template)
{
if ($this->_smarty->is_cached($template))
{
return true;
}

return false;
}

public function setCaching($caching)
{
$this->_smarty->caching = $caching;
}
}

That was the complete code of my Travello_View_Smarty class.

How to use the class

The usage of this class is quite simple. In your bootstrap file you can initialize it like this. The $viewConfig is used to setup the Zend_View paths. After creation the object is registered in the object store for later usage.

$viewConfig = array();
$viewConfig['scriptPath'] = $config->getSetting('framework', 'view_dir');

$view = new Travello_View_Smarty($viewConfig);
Zend::register('view', $view);

Within your controller an action method could look like this.

    public function indexAction()
{
$temp_file = 'homepage.htm';

$view = Zend::registry('view');

if (false === $view->isCached($temp_file))
{
$vars = array();
$vars['title' ] = 'Page <title>';
$vars['text' ] = 'A text is a text & a text is a text.';
$vars['numbers'][0] = 12.9;
$vars['numbers'][1] = 29;
$vars['numbers'][2] = 78;

$vars = $view->escape($vars);

$view->assign($vars);
}

$view->output($temp_file);
}

Conclusion

That was basically it. I have a couple of ideas how to extend this solution, but it already does the work for me. Any comments are welcome.

smarty = false;

public function __construct($data = array())
{
parent::__construct($data);

$config = Zend::registry('config');

$this->_smarty = new Smarty();

$this->_smarty->caching = $config->getSetting('smarty', 'caching');
$this->_smarty->cache_lifetime = $config->getSetting('smarty', 'cache_lifetime');
$this->_smarty->template_dir = $config->getSetting('smarty', 'template_dir');
$this->_smarty->compile_dir = $config->getSetting('smarty', 'compile_dir');
$this->_smarty->config_dir = $config->getSetting('smarty', 'config_dir');
$this->_smarty->cache_dir = $config->getSetting('smarty', 'cache_dir');
}

Implement _run() method

The method _run() is the only method that needs to be implemented in any subclass of Zend_View_Abstract. It is called automatically within the render() method. My implementation just uses the display() method from Smarty to generate and output the template.

___FCKpd___2

Overwrite assign() method

The next part is an overwrite of the assign() method from Zend_View_Abstract, which works in a similar way. The big difference is that the values are assigned to the Smarty object and not to the $this->_vars variables array of Zend_View_Abstract.

___FCKpd___3

Overwrite escape() method

The next part is an overwrite of the escape() method from Zend_View_Abstract. It works both for string and array values and also uses the escape() method from the Zend_View_Abstract. The advantage of this is that I don't have to care about each value of an array to get properly escaped.

___FCKpd___4

Print the output

The next method output() is a wrapper on the render() method from Zend_View_Abstract. It just sets some headers before printing the output.

___FCKpd___5

Use Smarty caching

The last two methods were created to simply integrate the Smarty caching mechanism in the View class. With the first one you can check for cached template and with the second one you can set the caching on or of.

___FCKpd___6

That was the complete code of my Travello_View_Smarty class.

How to use the class

The usage of this class is quite simple. In your bootstrap file you can initialize it like this. The $viewConfig is used to setup the Zend_View paths. After creation the object is registered in the object store for later usage.

___FCKpd___7

Within your controller an action method could look like this.

___FCKpd___8

Conclusion

That was basically it. I have a couple of ideas how to extend this solution, but it already does the work for me. Any comments are welcome.

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部