实现一个与Zend_View兼容的模板系统是很简单的。你只需要实现Zend_View_Interface接口即可,该接口定义了要实现兼容的最低要求。
定义一个Smarty.php,放在Zend/View/下
<?php
require_once('Smarty/Smarty.class.php');
require_once('Interface.php');
/**
* @copyright 2008 -
* @author cooldark
* @access public
* @todo 整合Smarty模板到Zend_View,代替Zend本身的模板
*
*/
class Zend_View_Smarty implements Zend_View_Interface {
/**
* Smarty对象
*
* @var Smarty
*/
private $_smarty;
/**
* 构造函数,初始化
*
* @param String $_smartyPath
* @param Array $_smartyParams
*/
public function __construct($_smartyPath = null, $_smartyParams = array()) {
$this -> _smarty = new Smarty();
if (null != $_smartyPath) {
$this -> setScriptPath($_smartyPath);
}
foreach ($_smartyParams as $key => $value) {
$this->_smarty->$key = $value;
}
}
/**
* 获得对象视图引擎并返回
*
* @return Smarty
*/
public function getEngine() {
return $this -> _smarty;
}
/**
* 设置模板文件所在目录
*
* @param String $path
*/
public function setScriptPath($path) {
if (is_readable($path)) {
$this -> _smarty -> template_dir = $path;
}
throw new Exception('Invalid path provided!');
}
/**
* 获取模板文件所在目录
*
* @return Array
*/
public function getScriptPaths() {
return array($this -> _smarty -> template_dir);
}
public function setBasePath($path, $prefix = 'Zend_View') {
return $this -> setScriptPath($path);
}
public function addBasePath($path, $prefix = 'Zend_View') {
return $this -> setScriptPath($path);
}
public function __set($key, $val) {
$this -> _smarty -> assign($key, $val);
}
public function __get($key) {
return $this -> _smarty -> get_template_vars($key);
}
public function __isset($key) {
return (null !== $this -> _smarty -> get_template_vars($key));
}
public function __unset($key) {
$this -> _smarty -> clear_assign($key);
}
