`
阿尔萨斯
  • 浏览: 4181169 次
社区版块
存档分类
最新评论

Integrating Smarty with the Zend Framework

 
阅读更多
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 $_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.

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-&gt;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-&gt;escape($vars); $view-&gt;assign($vars); } $view-&gt;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.

Updated on April 18, 2006

Please have also a look at the follow-up article on Integrating Sarty and ez Components with the Zend Framework.

Digg This!

Comments
Tuesday, April 4, 2006
RELATED ARTICLE ON SMARTY CACHING
1:59PM PDT · Jayson Minard (editor)
Wednesday, April 5, 2006
CONFIG CLASS
2:24AM PDT · jmkeith
CONFIG CLASS
3:03AM PDT · Ralf Eggert
GRMBL
3:05AM PDT · Ralf Eggert
SMARTY DESING AND INTERNATIONALIZATION
5:53AM PDT · hzubieta
Sunday, April 9, 2006
SMARTY AND HELPERS
11:30AM PDT · octavian82
RE: SMARTY DESING AND INTERNATIONALIZATION
9:27PM PDT · Ralf Eggert
RE: SMARTY AND HELPERS
9:30PM PDT · Ralf Eggert
Monday, May 29, 2006
GREAT TUTORIAL
4:12AM PDT · tlmarker
Thursday, June 8, 2006
PRETTY GOOD, BUT THERE ARE SOME ISSUES.
11:39PM PDT · crocodile2u
Monday, June 12, 2006
RUN.PHP ?
9:05AM PDT · myaticav
Wednesday, June 13, 2007
ZEND_VIEW_SMARTY
1:16PM PDT · scubamatrix
Tuesday, June 19, 2007
DESIGN HELP
6:09AM PDT · inter81
DESIGN HELP
6:10AM PDT · inter81
DESIGN HELP
6:11AM PDT · inter81
Thursday, August 2, 2007
INTEGRATING TEMPLATELITE WITH ZEND VIEW
7:26AM PDT · borec86
Sunday, August 26, 2007
HTTP://WWW.UNIXVPS.COM
7:28AM PDT · unixvps
Thursday, August 30, 2007
GREGORY SZORC
1:00PM PDT · codemann
Friday, September 14, 2007
SMARTY
10:45PM PDT · Lex [unregistered]
Monday, November 12, 2007
ZEND_VIEW_ABSTRACT::_RUN() DOES NOT ACCEPT ANY ARGUMENTS
6:59PM PST · dubaut
Sunday, March 30, 2008
CAN YOU WRITE
1:28PM PDT · bububgirl
Saturday, April 19, 2008
USEFULL ARTICLE !! I'VE TRANSLATED THIS ARTICLE INTO CHINESE.
9:37PM PDT · artlover
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics