nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY MAURICE FONK ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Naneau * @package Naneau_View * @copyright Copyright (c) 2007 Maurice Fonk - http://naneau.nl * @version 0.3 */ /** * Zend View Base Class */ require_once 'Zend/View.php'; /** * Smarty templating engine */ require_once 'Smarty/Smarty.class.php'; /** * @category Naneau * @package Naneau_View * @copyright Copyright (c) 2007 Maurice Fonk - http://naneau.nl */ class Naneau_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() * @see Zend_View_Abstract::getVars() * @param string $name the template * @return void */ protected function _run() { $this->strictVars(true); //force strict handling of variables foreach ($this->getVars() as $key => $value) { //for all the variables assigned to the view $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 = array_shift($this->getScriptPaths()); //the path where templates are located $file = substr(func_get_arg(0), strlen($path)); //smarty needs a template_dir, and can only use templates found in that //directory, so we have to strip it from the filename if (strstr($file, '/')) { //if file is in subdir, update template path $path .= substr($file, 0, strrpos($file, '/')) . '/'; //update path to include any dirs in $file $file = substr($file, strrpos($file, '/') + 1); //update $file to _not_ include those dirs } //update the path and file, to comply with smarty's {include} usage $this->_smarty->template_dir = $path; //set the template directory $compileID = md5($path . $file); //a unique compile ID (in case of multiple templates by the same name) echo $this->_smarty->fetch($file, null, $compileID); //process the template (and filter the output) } }