<?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() {} }
It looks like you're new here. If you want to get involved, click one of these buttons!