Smarty + Zend_View, take three
Tuesday, July 31, 2007 10:21:01 PM
So here's take three on integrating Smarty with Zend_View, this time hopefully staying useful for a longer time since the Zend Framework has reached version 1 now.
In this post I will show you a way to integrate Smarty with Zend_View as a new subclass and how to use this class with the new ViewRenderer helper to automatically display templates.
So let's begin with the implementation of SmartyView, the new class based on Zend_View_Abstract that uses Smarty as the template engine.
<?php
class SmartyView extends Zend_View_Abstract
{
protected $_smarty;
public function __construct($config = array())
{
$this->_smarty = new Smarty();
if(!isset($config['compileDir']))
throw new Exception('compileDir is not set for '.get_class($this));
else
$this->_smarty->compile_dir = $config['compileDir'];
if(isset($config['configDir']))
$this->_smarty->config_dir = $config['configDir'];
if(isset($config['pluginDir']))
$this->_smarty->plugin_dir[] = $config['pluginDir'];
parent::__construct($config);
}
public function getEngine()
{
return $this->_smarty;
}
public function __set($key,$val)
{
$this->_smarty->assign($key,$val);
}
public function __isset($key)
{
$var = $this->_smarty->get_template_vars($key);
if($var)
return true;
return false;
}
public function __unset($key)
{
$this->_smarty->clear_assign($key);
}
public function assign($spec,$value = null)
{
if($value === null)
$this->_smarty->assign($spec);
else
$this->_smarty->assign($spec,$value);
}
public function clearVars()
{
$this->_smarty->clear_all_assign();
}
protected function _run()
{
$this->strictVars(true);
$this->_smarty->assign_by_ref('this',$this);
$templateDirs = $this->getScriptPaths();
$file = substr(func_get_arg(0),strlen($templateDirs[0]));
$this->_smarty->template_dir = $templateDirs[0];
$this->_smarty->compile_id = $templateDirs[0];
echo $this->_smarty->fetch($file);
}
}
?>
Now that we have that, it's only a small snippet of code to our index.php / front controller file to do the rest:
//Create the view and set the compile dir to template_c
$view = new SmartyView(array(
'compileDir' => './template_c'
));
//Create a new ViewRenderer helper and assign our newly
//created SmartyView object as the view instance
$viewHelper = new Zend_Controller_Action_Helper_ViewRenderer($view);
$viewHelper->setViewSuffix('tpl');
//Save the helper to the HelperBroker
Zend_Controller_Action_HelperBroker::addHelper($viewHelper);
With that code the view will autorender your templates. Also, you can assign the smarty instance as a parameter to the request and still use it as you would normally use Smarty and assign template variables etc. and they will be reflected in the automatically rendered template.
That is all. Simpler than you expected?







Anonymous # Sunday, September 23, 2007 2:14:42 PM
Anonymous # Tuesday, October 2, 2007 9:08:16 PM
Janizomg # Tuesday, October 2, 2007 9:16:04 PM
Add this to SmartyView class, inside _run function after $templateDirs = $this->getScriptPaths();
Anonymous # Tuesday, October 2, 2007 9:28:31 PM
Anonymous # Friday, October 12, 2007 7:53:56 AM
Anonymous # Thursday, November 29, 2007 11:00:14 PM
Janizomg # Friday, November 30, 2007 4:43:47 AM
Anonymous # Thursday, December 6, 2007 10:39:11 PM
Anonymous # Thursday, December 6, 2007 11:24:05 PM
Janizomg # Sunday, December 9, 2007 3:03:35 PM
Anonymous # Sunday, December 16, 2007 5:25:07 PM
Anonymous # Thursday, February 14, 2008 7:23:12 PM
Anonymous # Thursday, February 14, 2008 7:51:08 PM
Anonymous # Monday, March 10, 2008 10:14:03 PM
Anonymous # Tuesday, September 30, 2008 3:16:06 PM
Anonymous # Monday, January 25, 2010 8:19:28 PM
Anonymous # Friday, June 11, 2010 2:13:58 PM
Anonymous # Tuesday, September 7, 2010 1:25:05 PM