I've seen few tutorials on this, but they all seem to take rather strange approach and sometimes not very compatible with Zend View system. I wanted to share my 2 classes I created for this. Hope someone will find these usefule:
PHP Code:
<?php
require_once 'Zend/View/Abstract.php';
require_once 'Smarty/Smarty.class.php';
class Ant_View_Smarty extends Zend_View_Abstract
{
/**
* Smarty object
* @var Smarty
*/
protected $_smarty;
/**
* Constructor
*
* @param string $tmplPath
* @param array $extraParams
* @return void
*/
public function __construct($config = array())
{
parent::__construct($config);
$this->_smarty = new Smarty;
$this->_smarty->template_dir = DIR_CMS . 'application';
$this->_smarty->compile_dir = DIR_CMS . 'templates_c';
$this->_smarty->assign_by_ref ('view', $this);
$this->_smarty->compile_check = true;
$this->_smarty->debugging = true;
$reg = Zend_Registry::getInstance();
$this->_smarty->assign_by_ref('registry', $reg);
}
/**
* Return the template engine object
*
* @return Smarty
*/
public function getEngine()
{
return $this;
}
/**
* Assign a variable to the template
*
* @param string $key The variable name.
* @param mixed $val The variable value.
* @return void
*/
public function __set($key, $val)
{
$this->_smarty->assign($key, $val);
}
/**
* Retrieve an assigned variable
*
* @param string $key The variable name.
* @return mixed The variable value.
*/
public function __get($key)
{
return $this->_smarty->get_template_vars($key);
}
/**
* Allows testing with empty() and isset() to work
*
* @param string $key
* @return boolean
*/
public function __isset($key)
{
return (null !== $this->_smarty->get_template_vars($key));
}
/**
* Allows unset() on object properties to work
*
* @param string $key
* @return void
*/
public function __unset($key)
{
$this->_smarty->clear_assign($key);
}
/**
* Assign variables to the template
*
* Allows setting a specific key to the specified value, OR passing an array
* of key => value pairs to set en masse.
*
* @see __set()
* @param string|array $spec The assignment strategy to use (key or array of key
* => value pairs)
* @param mixed $value (Optional) If assigning a named variable, use this
* as the value.
* @return void
*/
public function assign($spec, $value = null)
{
if (is_array($spec)) {
$this->_smarty->assign($spec);
return;
}
$this->_smarty->assign($spec, $value);
}
/**
* Clear all assigned variables
*
* Clears all variables assigned to Zend_View either via {@link assign()} or
* property overloading ({@link __get()}/{@link __set()}).
*
* @return void
*/
public function clearVars()
{
$this->_smarty->clear_all_assign();
}
/**
* Processes a template and returns the output.
*
* @param string $name The template to process.
* @return string The output.
*/
public function render($name)
{
$this->_file = $this->_script($name);
unset ($name);
return $this->_smarty->fetch($this->_file);
}
function _run ()
{
}
}
?>
PHP Code:
<?php
require_once ('Zend/Controller/Action.php');
/**
* Is same as Zend_Controller_Action, but
* uses smarty view system instead of Zend_View.
*
*/
class Ant_Controller_Action extends Zend_Controller_Action
{
/**
* Initalizes the view mechanism if needed and returns instance
* of the engine.
*
* @return Zend_View_Smarty
*/
function initView ()
{
require_once 'Zend/View/Interface.php';
if (isset($this->view) && ($this->view instanceof Zend_View_Interface)) {
return $this->view;
}
$this->viewSuffix = 'tpl';
$request = $this->getRequest();
$module = $request->getModuleName();
$dirs = $this->getFrontController()->getControllerDirectory();
if (empty($module) || !isset($dirs[$module])) {
$module = 'default';
}
$baseDir = dirname($dirs[$module]) . DIRECTORY_SEPARATOR . 'views';
if (!file_exists($baseDir) || !is_dir($baseDir)) {
throw new Zend_Controller_Exception('Missing base view directory ("' . $baseDir . '")');
}
require_once 'Ant/SmartyView.php';
$this->view = new Ant_View_Smarty(array('basePath' => $baseDir));
return $this->view;
}
}
?>
You put Smarty into library folder and have to create a folder where templates are to be compiled. In View_Smarty constructor make sure you specify the right paths for smarty.
Then you derive your controllers from Ant_Controller_Action and use it as normal.