By Tom Anderson toma

Zend Framework provides a means of creating your own resource plugins to use with Zend_Application. http://framework.zend.com/manual/en/zend.application.theory-of-operation.html#zend.application.theory-of-operation.resources for more details [funny quotes to live by].

This article is the code and ini settings for implementing Smarty in your bootstrap.

The directory structure:

-- application
   -- configs
      -- application.ini 
   -- controllers 
      -- default
         -- IndexController.php
   -- plugins
      -- smarty This is the best place I found for storing application specific plugins
   -- views
      -- smarty These are views used by application specific plugins
      -- default
         -- index
             -- index.tpl
-- library
   -- Smarty-3.0.4
   -- Zend
   -- Toma
      -- Application
         -- Resource
            -- Smarty.php
      -- Controller
         -- Action.php
-- media
   -- compile
   -- cache

Note the views and controllers directories. I prefer to store my views under a common views directory instead of individually with each module. My exception to this rule is application plugins which is a whole other discussion. Controllers are stored under their module and views mirror the module/controller/action path [funny jokes].

These INI settings should be added to your application.ini

;; Path for smarty application resource
pluginpaths.Toma_Application_Resource = APPLICATION_PATH "/../library/Toma/Application/Resource"

resources.smarty.path = APPLICATION_PATH "/../library/Smarty-3.0.4/libs/Smarty.class.php"
resources.smarty.template_dir[] = APPLICATION_PATH "/views/"
resources.smarty.compile_dir = APPLICATION_PATH "/../media/compile/"
resources.smarty.cache_dir = APPLICATION_PATH "/../media/cache/"
resources.smarty.plugins_dir[] = APPLICATION_PATH "/../library/Smarty-3.0.4/libs/plugins/"
resources.smarty.plugins_dir[] = APPLICATION_PATH "/plugins/smarty/"
resources.smarty.debug_tpl = APPLICATION_PATH "/views/debug.tpl"
resources.smarty.debugging = false
resources.smarty.allow_php = false
resources.smarty.caching = false
resources.smarty.cache_modified_check = true
resources.smarty.cache_lifetime = 86400
resources.smarty.compile_check = true
resources.smarty.force_compile = false

;; Strongly recommended
resources.frontController.params.noViewRenderer = true

This code should be saved as /library/Toma/Application/Resource/Smarty.php

<?php
/**
 * Toma_Application_Resource_Smarty
 * 
 * @author tom.h.anderson@gmail.com
 * @date 2010-11-23
 */

class Toma_Application_Resource_Smarty extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * @var Smarty
     */
    protected $_smarty;

    /**
     * init - Required from the abstract
     *
     * @return Smarty
     */
    public function init()
    {
        return $this->initSmarty();
    }

    /**
     * initSmarty
     * Read the ini options and return
     * an instance of Smarty or 
     * $this->_smarty
     */
    public function initSmarty() {
        if ($this->_smarty) return $this->_smarty;
        $options = $this->getOptions();

        require_once($options['path']);
        unset($options['path']);

        // Create smarty object.
        $t = new Smarty();
        foreach ($options as $key => $val) {
            $t->$key = $val;
        }

        $this->_smarty = $t;
        return $t;
    }

    /**
     * Return an instance of Smarty
     */
    public function getSmarty() {
        return $this->initSmarty();
    }
}

Optionally add to your bootstrap (in case you want to add it to the registry):

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initSmarty()
    {
        $smarty = $this->getPluginResource('smarty')->getSmarty();
        Zend_Registry::set('smarty', $smarty);

        return $smarty;
    }
}

And added to your extension of Zend_Controller_Action:

class Toma_Controller_Action extends Zend_Controller_Action {
    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array()) 
    {
        parent::__construct($request, $response, $invokeArgs);

        $bootstrap = $invokeArgs['bootstrap'];
        $this->view = $bootstrap->getPluginResource('smarty')->getSmarty();
    }
}

Once all the above code is in place you should be able to write a controller as: [hilarious quotes]

class Default_IndexController extends Toma_Controller_Action
{
    public function indexAction()
    {
        $this->view->display('default/index/index.tpl');
    }
}

Questions and comments about this article