Integrating Smarty with the Zend Framework
/Travello/View/Smarty.php
<?
php
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 ' );
}
protected function _run( $template )
{
$this -> _smarty -> display( $template );
}
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 ));
}
}
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 ;
}
}
/*
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.
1.initialize
$viewConfig = array();
$viewConfig['scriptPath'] = $config->getSetting('framework', 'view_dir');
$view = new Travello_View_Smarty($viewConfig);
Zend::register('view', $view);
2.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
*/
?>
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 ' );
}
protected function _run( $template )
{
$this -> _smarty -> display( $template );
}
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 ));
}
}
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 ;
}
}
/*
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.
1.initialize
$viewConfig = array();
$viewConfig['scriptPath'] = $config->getSetting('framework', 'view_dir');
$view = new Travello_View_Smarty($viewConfig);
Zend::register('view', $view);
2.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
*/
?>