Hi ive managed to integrate 3 templating engines which are Smarty , Template Lite and PEAR's HTML_Template_Flexy which is the best. The smarty examples out there are broken and out of date, it took a while to find the right one. Let me know if you need a hand with this.

Here is my version, let me know if this is what you are after or if this version is aswell out of date !!!!

The problem I may have with this is, an application of ours needs templates from different directories from a given session variable ie site1, site2, site3 etc so there is no easy way to dynamically change the view "script" template location unless there is a way ?

class Zend_View_Smarty extends Zend_View_Abstract
{
   /**
    * Smarty object
    * @var Smarty
    */
   protected $_smarty;

   /**
    * Constructor
    *
    * Pass it a an array with the following configuration options:
    *
    * scriptPath: the directory where your templates reside
* compileDir: the directory where you want your compiled templates (must be
    * writable by the webserver)
    * configDir: the directory where your configuration files reside
    *
    * both scriptPath and compileDir are mandatory options, as Smarty needs
    * them. You can't set a cacheDir, if you want caching use Zend_Cache
    * instead, adding caching to the view explicitly would alter behaviour
    * from Zend_View.
    *
    * @see Zend_View::__construct
    * @param array $config
    * @throws Exception
    */
   public function __construct($config = array())
   {
       $this->_smarty = new Smarty();
       //smarty object

       if (!array_key_exists('compileDir', $config)) {
throw new Exception('compileDir must be set in $config for ' . get_class($this));
       } else {
           $this->_smarty->compile_dir = $config['compileDir'];
       }
       //compile dir must be set

       if (array_key_exists('configDir', $config)) {
           $this->_smarty->config_dir = $config['configDir'];
       }
       //configuration files directory

       parent::__construct($config);
       //call parent constructor
   }

   /**
    * Return the template engine object
    *
    * @return Smarty
    */
   public function getEngine()
   {
       return $this->_smarty;
   }

   /**
    * fetch a template, echos the result,
    *
    * @see Zend_View_Abstract::render()
    * @param string $name the template
    * @return void
    */
   protected function _run()
   {
       $this->strictVars(true);

       $vars = get_object_vars($this);
       foreach ($vars as $key => $value) {
           if ('_' != substr($key, 0, 1)) {
               $this->_smarty->assign($key, $value);
           }
       }
       //assign variables to the template engine
       $this->_smarty->assign_by_ref('this', $this);
       //why 'this'?
       //to emulate standard zend view functionality
       //doesn't mess up smarty in any way

       $path = $this->getScriptPaths();
$file = substr(func_get_arg(0), strlen($path[0]));
       //smarty needs a template_dir, and can only use templates,
       //found in that directory, so we have to strip it from the filename

       $this->_smarty->template_dir = $path[0];
       //set the template diretory as the first directory from the path

       echo $this->_smarty->fetch($file);
       //process the template (and filter the output)
   }
}


mbneto wrote:
Hi,

I've been using Zend with my projects since 0.7 and using a Smarty class with the View to allow me to $this->view->render using the templates.

I've upgraded to Zend 1.0.0 and all my pages broke with a fatal error message

*Fatal error*: Class Foo_View_Smarty contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Zend_View_Interface::getScriptPaths, Zend_View_Interface::setBasePath, Zend_View_Interface::addBasePath)

The class code is listed bellow but after searching for a while I saw some posts regarding the ViewRenderer. Since I'll have to recode some files I am looking for a 'definitive' way to integrate Smarty with Zend without having to wait until Zend_Layout appears...

class Foo_View_Smarty implements Zend_View_Interface
{
    protected $_smarty;
public function __construct($path = null, array $config = array()) { $this->_smarty = new Smarty; if ($path !== null)
        {
            $this->setScriptPath($path);
        }

        foreach ($config as $key => $value)
        {
            $this->_smarty->$key = $value;        }
}
  public function getEngine()
    {        return $this->_smarty;
}
  public function setScriptPath($path)
{ if (!Zend_Loader::isReadable($path)) { throw new Zend_View_Exception("Unable to read script path: {$path}"); } $this->_smarty->template_dir = $path; }
  public function __set($key, $value)
    {        $this->_smarty->assign($key, $value);
}
   public function __get($key)
    {
        return $this->_smarty->get_template_vars($key);
    }

    public function __isset($key)
    {
        return $this->_smarty->get_template_vars($key) !== null;
    }
public function __unset($key)
    {
        $this->_smarty->clear_assign($key);
    }

public function assign($spec, $value = null)
    {

        if (is_array($spec))
        {
            $this->_smarty->assign($spec);
        }
        else
        {
            $this->_smarty->assign($spec, $value);
        }
    }

    public function clearVars()
    {
        $this->_smarty->clear_all_assign();
    }

    public function render($name)
    {
        return $this->_smarty->fetch($name);
    }
public function fetch($name)
    {
        return $this->_smarty->fetch($name);
    }
}

Reply via email to