<?php
/**
*
* Zend Framework 与 PHP模板引擎SMARTY 的配置
*
*
*/
require_once 'Smarty/Smarty.class.php';
class Custom_View_Smarty implements Zend_View_Interface
{
/**
* Smarty object
* @var Smarty
*/
protected $_smarty;
/**
* Constructor
*
* @param string $tmplPath
* @param array $extraParams
* @return void
*/
public function __construct(array $smartyParams = array())
{
//检测SMARTY配置参数是否合格
$this->_checkRequiredOptions($smartyParams);
$this->_smarty = new Smarty;
foreach ((array) $smartyParams as $key => $value)
{
$this->_smarty->$key = $value;
}
}
/**
* Return the template engine object
* 返回模板引擎对象
* @return Smarty
*/
public function getEngine()
{
return $this->_smarty;
}
public function setScriptPath($path)
{
}
public function getScriptPaths()
{
}
/**
* Set a base path to all view resources
*
* @param string $path
* @param string $classPrefix
* @return void
*/
public function setBasePath($path, $classPrefix = 'Zend_View')
{
}
/**
* Add an additional path to view resources
*
* @param string $path
* @param string $classPrefix
* @return void
*/
public function addBasePath($path, $classPrefix = 'Zend_View')
{
}
/**
* 设置模板路径
*
* @param string $path The directory to set as the path.
* @return void
*/
public function setTemplatePath()
{
if (is_readable( $this->_smarty->template_dir ))
{
return $this->_smarty->template_dir;
}
throw new Exception('Invalid TemplatePath provided');
}
/**
* 设置模板编译路径
*
* @param string $path The directory to set as the path.
* @return void
*/
public function setCompilePath()
{
if (is_readable( $this->_smarty->compile_dir))
{
return $this->_smarty->compile_dir;
}
throw new Exception('Invalid CompilePath provided');
}
public function getTemplatePath()
{
return $this->_smarty->template_dir;
}
/**
* 分配变量给模板 Assign a variable to the template
*
* @param string $key The variable name.
* @param mixed $val The variable value.
* @return void
*/
public function __set($key, $val)
{
$this->_smarty->assign($key, $val);
}
/**
* Retrieve an assigned variable
*
* @param string $key The variable name.
* @return mixed The variable value.
*/
public function __get($key)
{
return $this->_smarty->get_template_vars($key);
}
/**
* Allows testing with empty() and isset() to work
*
* @param string $key
* @return boolean
*/
public function __isset($key)
{
return (null !== $this->_smarty->get_template_vars($key));
}
/**
* Allows unset() on object properties to work
*
* @param string $key
* @return void
*/
public function __unset($key)
{
$this->_smarty->clear_assign($key);
}
/**
* 分配变量给模板 Assign variables to the template
*
* Allows setting a specific key to the specified value, OR passing an array
* of key => value pairs to set en masse.
*
* @see __set()
* @param string|array $spec The assignment strategy to use (key or array of key
* => value pairs)
* @param mixed $value (Optional) If assigning a named variable, use this
* as the value.
* @return void
*/
public function assign($spec, $value = null)
{
if (is_array($spec))
{
$this->_smarty->assign($spec);
return;
}
$this->_smarty->assign($spec, $value);
}
/**
* 清楚所有已经分配变量 Clear all assigned variables
*
* Clears all variables assigned to Zend_View either via {@link assign()} or
* property overloading ({@link __get()}/{@link __set()}).
*
* @return void
*/
public function clearVars()
{
$this->_smarty->clear_all_assign();
}
/**
* 处理模板并返回输出的内容 Processes a template and returns the output.
*
* @param string $name The template to process.
* @return string The output.
*/
public function render($name)
{
return $this->_smarty->fetch($name);
}
/**
* 处理模板并显示输出内容 Processes a template and display the output.
*
* @param string $name The template to process.
* @return string The output.
*/
public function display($name)
{
return $this->_smarty->display($name);
}
/**
* Check for extraParams options that are mandatory.
* 检测 extraParams 的参数项
* Throw exceptions if any are missing.
* 抛出错误异常
* @param array $extraParams
* @throws Zend_View_Exception
*/
protected function _checkRequiredOptions(array $extraParams)
{
// we need at least a template_dir and compile_dir
if (! array_key_exists('template_dir', $extraParams)) {
require_once 'Zend/View/Exception.php';
throw new Zend_View_Exception("Configuration array must have a key for 'template_dir' for Smarty View.",$this);
}
if (! array_key_exists('compile_dir', $extraParams)) {
/**
* @see Zend_View_Exception
*/
require_once 'Zend/View/Exception.php';
throw new Zend_View_Exception("Configuration array must have a key for 'compile_dir' for Smarty Compile.",$this);
}
}
}
?>