Getting Smarty 3.0 to work with Zend Framework 1.9

Assuming you’re using the method demonstrated in the Zend Framework Manual, the first thing you need is to configure the Zend AutoLoader. Add this to the index.php file in your public directory:

// Configure autoloader
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();

// Require Smarty Engine
require_once 'Smarty/Smarty.class.php'; // Assuming that you have the Smarty engine at "library/Smarty"
$autoloader->pushAutoloader('Smarty', 'smartyAutoload');

Change the following line in “Smarty/Smarty.class.php” (about line 91) from this:

if (set_include_path(SMARTY_SYSPLUGINS_DIR . PATH_SEPARATOR . get_include_path()) !== false) {

To this:

if (!class_exists('Zend_Loader_Autoloader') && set_include_path(SMARTY_SYSPLUGINS_DIR . PATH_SEPARATOR . get_include_path()) !== false) {

Next, comment out the line in the Smarty Constructor (about line 237):

//$this->template_dir = array('.' . DS . 'templates' . DS);

We want to keep it a null value (which is the default). If we don’t make this change in the constructor (mentioned just above), we’ll get PHP Notices that Zend_Controller_Action_Helper_ViewRenderer tried to convert an array to a string.

Finally, add this to your bootstrapper class:

protected function _initView()
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(‘ViewRenderer’);
$view = new Zend_View_Smarty();

->setViewScriptPathSpec(APPLICATION_PATH . ‘/views/scripts/:controller/:action.:suffix’)

If you’re using modules, you might want to make a slight change to the setViewScriptPathSpec method:

setViewScriptPathSpec(APPLICATION_PATH . ‘/modules/:module/views/scripts/:controller/:action.:suffix’);

You should be good to go. I should mention that this will cause your layouts to be parsed via Smarty as well; which presents its own problems. A potential work around is to include a common header and footer for each individual template, although this is far from ideal. In that case, what you might want to do is disable the view renderer’s auto render feature and create a custom plugin or action helper that will render the templates and layouts. I’m considering expounding on this in another post…

4 thoughts on “Getting Smarty 3.0 to work with Zend Framework 1.9”

  1. $view = new Easyworship_View_Smarty();

    This is referenced above, but you do not show what it is or where it comes from. Am I missing something?

  2. Oops, sorry. Fixed now. It should have been “$view = new Zend_View_Smarty();”. I also added a mention regarding assumptions.

  3. Hi, I’m quite new to the Zend Framework, and must admit this tutorial has helped with the smarty integration, so thank you. I’ve managed to replace all the index.phtml and error.phtml with their smarty equivalents, but when I try for e.g in index.tpl: {include file=”test.tpl”} (where test.tpl is located in the same directory as index.tpl), I get an error: “Unable to load template file ‘test.tpl’” (which is what is being caught in the error.tpl). Any help with including smarty tpl’s within tpl’s then, using this integration method?

  4. @Paddy, the files will be included based on the include paths have set via “ini_set” (i.e. ini_set(‘include_path’, ‘/my/inc/path’)). Just modify the path in your include statement to something like: {include file=”application/modules/default/views/index/test.tpl”}

Comments are closed.