ZF refactoring and CampSite template_engine/Smarty
  • Having a look at the current state of the devel branch of Newscoop makes me curious about the ZendFramework refactoring strategy.

    According to the ZF & Doctrine page in the wiki and the .phtml views in the admin module, PHP is being used as the default template system.

    Are you planning on integrating Smarty with Zend_View (at least for the default/frontend module) at some point?

    Maybe you can point me towards some relevant wiki entries I have not found yet?


    Cheers,
    Leander
  • 2 Comments sorted by
  • Hi Leander,

    Bad luck, that's one of the questions without any answer at this point
    :) But yeah, I think that's a good option as we are already using
    Smarty without any issues, there are a few more options though. You
    will get news once we make some decisions. Thanks.

    Best,


    On Sat, May 21, 2011 at 8:44 AM, Leander Damme
    <newscoop-dev@lists.sourcefabric.org> wrote:
    >
    > Having a look at the current state of the devel branch of Newscoop makes me curious about the ZendFramework refactoring strategy.
    >
    > According to the ZF & Doctrine page in the wiki and the .phtml views in the admin module, PHP is being used as the default template system.
    >
    > Are you planning on integrating Smarty with Zend_View (at least for the default/frontend module) at some point?
    >
    > Maybe you can point me towards some relevant wiki entries I have not found yet?
    >
    >
    > Cheers,
    > Leander
    >


    --
    Holman Romero
    Senior Software Engineer, Sourcefabric
    holman.romero@sourcefabric.org

    Salvátorská 10
    110 00 Praha 1, Czech Republic
    +420 608910633

    http://www.sourcefabric.org
  • See attached a quick and dirty approach to integrate Smarty with Zend_View
    I have been using this wrapper in combination with smarty3 for quite some time now. Also let´s you use Zend_View_Helpers in smarty templates seamlessly.
    I am curious about the other options though Wink

    Best,
    Leander

    <?php
    /***************************************************************************
     * Smarty Zend View Wrapper
     * see http://www.jurajsplayground.com/blog/2010/04/04/...
     * ...zend-framework-smarty-with-zend-view-helpers/ for more information, too
     ***************************************************************************/
    
    require_once "Zend/View/Abstract.php";
    require_once "Zend/View/Interface.php";
    require_once 'Smarty/Smarty.class.php';
    
    /*
     * Extend Zend_View_Abstract with Smarty
     */
    class Newscoop_SmartyView 
    	extends Zend_View_Abstract 
    		 implements Zend_View_Interface
    {
    	/**
    	 * @var Smarty
    	 */
    	protected $_smarty;
    
    	/**
    	 * Constructor
    	 *
    	 * @param array $config
    	 * @return void
    	 */
    	public function __construct($config = array()) {
    		
    		$this->_smarty = new Smarty();
    		
    		if (!isset($config['compile_dir'])) {
    			throw new Newscoop_Exception('compile_dir must be set in $config for ' . get_class($this));
    		} else {
    			$this->_smarty->compile_dir = $config['compile_dir'];
    		}
    		if (isset($config['config_dir'])) {
    			$this->_smarty->config_dir = $config['config_dir'];
    		}
    		
    		parent::__construct($config);
    		
    	}
    	
    	/**
    	 * Registers additional modifiers
    	 * 
    	 * @return void
    	 */
    	protected function _registerModifiers() {
    		$this->_smarty->register_modifier('num', 'number_format');
    	}
    
    	/**
    	 * Return the template engine object
    	 *
    	 * @return Smarty
    	 */
    	public function getEngine() {
    		return $this->_smarty;
    	}
    
    	/**
    	 * Set the path to the templates
    	 *
    	 * @param string $path The directory to set as the path.
    	 * @return void
    	 */
    	public function setScriptPathBck($path) {
    		if (is_readable($path)) {
    			$this->_smarty->template_dir = $path;
    				return $this;
    		}
    		//throw new Newscoop_Exception('Invalid path provided');
    	}
    	
    	/**
    	 * Retrieve the current template directory
    	 *
    	 * @return string
    	 */
    	public function getScriptPathsBck() {
    		return array($this->_smarty->template_dir);
    	}
    	
    	/**
    	 * Alias for setScriptPath
    	 *
    	 * @param string $path
    	 * @param string $prefix Unused
    	 * @return void
    	 */
    	public function setBasePathBck($path, $prefix = 'Zend_View') {
    		return $this->setScriptPath($path);
    	}
    
    	/**
    	 * Alias for setScriptPath
    	 *
    	 * @param string $path
    	 * @param string $prefix Unused
    	 * @return void
    	 */
    	public function addBasePathBck($path, $prefix = 'Zend_View') {
    		return $this->setScriptPath($path);
    	}
    
    
    	/**
    	 * Smarty assign method (->)
    	 *
    	 * @param string $key
    	 * @param mixed $val
    	 * @return void
    	 */
    	public function __set($key, $val) {
    		$this->_smarty->assign($key, $val);
    	}
    
    	/**
    	 * Smarty get method
    	 *
    	 * @param string $key der Variablenname.
    	 * @return mixed der Variablenwert.
    	 */
    	public function __get($key) {
    		return $this->_smarty->getTemplateVars($key);
    	}
    
    	/**
    	 * Erlaubt das Testen von empty() und isset()
    	 *
    	 * @param string $key
    	 * @return boolean
    	 */
    	public function __isset($key) {
    		$var = $this->_smarty->getTemplateVars($key);
    		if ($var) {
            	return true;
    		}
            return false;
    	}
    
    	/**
    	 * unset method - clear assign
    	 *
    	 * @param string $key
    	 */
    	public function __unset($key) {
            $this->_smarty->clear_assign($key);
        }
    	
    	/**
    	 * Smarty assign method
    	 *
    	 *
    	 * @see __set()
    	 * @param string|array $spec
    	 * @param mixed $value (Optional) Wenn ein Variablenname verwendet wurde, verwende dies als den Wert.
    	 * @return void
    	 */
    	public function assign($spec, $value = null) {
    		if (is_array($spec))
    			$this->_smarty->assign($spec);
    		else
    			$this->_smarty->assign($spec, $value);
    	}
    	
    	/**
    	 * clearVars method - clear all assigns
    	 *
    	 */
    	public function clearVars() {
    		$this->_smarty->clear_all_assign();
    	}
    	
    
    	
    	/**
    	 * Smarty render method
    	 *
    	 * @param string $name Das zu verarbeitende Template
    	 * @param string $cacheId [optional]
    	 * @param string $compileId [optional]
    	 * @return string Die Ausgabe.
    	 */
    	public function render($name, $cacheId = null, $compileId = null) {
    		
    		$templateDirs = $this->getScriptPaths();
    		foreach ($templateDirs as $td) {
    			if (is_file($td.$name)) {
    				$this->_smarty->template_dir = $td;
    				$search = array('/scripts/admin/', '/scripts/');
    				$replace = array('', '');
    				$module = array_reverse(explode('/', str_replace($search, $replace, $td)));
    				$compileId = $module[0].'_'.$module[1].'_'.$name;
    				return $this->_smarty->fetch($name, $cacheId, $compileId);
    			}	
    		}
    		try {
    			$this->_smarty->compile_id = $name;
    			$fetch = $this->_smarty->fetch($name);
    			return $fetch;
    		} catch (Exception $e) {
    			throw new Zend_Exception('View Template not found!');	
    		}
    		
    	}
    	
    	/**
    	 * Enables Smarty caching
    	 *
    	 * @param integer $lifeTime in seconds
    	 * @param bool $global whether $lifeTime applies to all files
    	 * @return Jazz_View_Smarty
    	 */
    	public function enableCaching($lifeTime = 300, $global = true) {
    		$this->_smarty->caching = $global === true ? 1 : 2;
    		$this->_smarty->cache_lifetime = $lifeTime;
    		return $this;
    	}
    
    	/**
    	 * Disables Smarty caching
    	 *
    	 * @return Jazz_View_Smarty
    	 */
    	public function disableCaching() {
    		$this->_smarty->caching = 0;
    		return $this;
    	}
    
    	/**
    	 * Finds out if template is cached
    	 *
    	 * @param string $template Path to the template
    	 * @return boolean
    	 */
    	public function isCached($template, $cacheId = null, $compileId = null) {
    		return $this->_smarty->is_cached($template, $cacheId, $compileId);
    	}
    	
    	/**
    	 * Smarty run method
    	 */
    	public function _run() {}
    	
    	
    }