(1)入口文件:/public/index.php:
bootstrap()->run();
?>
(2)在引导程序Bootstrap.php中定义自已的视图引擎Smarty(/application/Bootstrap.php):
class Bootstrap extends Yaf_Bootstrap_Abstract{
public function _initConfig() {
//把配置保存起来
$arrConfig = Yaf_Application::app()->getConfig();
Yaf_Registry::set('config', $arrConfig);
}
//其他定义忽略......
public function _initSmarty(Yaf_Dispatcher $dispatcher) {
//init smarty view engine
$smarty = new Smarty_Adapter(null, Yaf_Registry::get("config")->get("smarty"));
$dispatcher->setView($smarty);
}
}
(3)添加Smarty的适配器即Smarty_Adapter类,使Yaf和Smarty之间能进行适配,把你需要view做的功能实现在Smarty_Adapter类里面实现,yaf通过他们来操作smarty的特性。
首先下载smarty源码包(如: smarty-2.6.29.tar.gz),将其上传到服务器指定位置,解压后将其复制到基类目录library下面,并命名为Smarty,然后在Smarty目录下新建Adapter.php文件,在其中添加如下内容:
vim Adapter.php
_smarty = new Smarty;
if (null !== $tmplPath) {
$this->setScriptPath($tmplPath);
}
foreach ($extraParams as $key => $value) {
$this->_smarty->$key = $value;
}
}
/**
* 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 setScriptPath($path)
{
if (is_readable($path)) {
$this->_smarty->template_dir = $path;
return;
}
throw new Exception('Invalid path provided');
}
/**
* Retrieve the current template directory
*
* @return string
*/
public function getScriptPath()
{
return $this->_smarty->template_dir;
}
/**
* Alias for setScriptPath
*
* @param string $path
* @param string $prefix Unused
* @return void
*/
public function setBasePath($path, $prefix = 'Zend_View')
{
return $this->setScriptPath($path);
}
/**
* Alias for setScriptPath
*
* @param string $path
* @param string $prefix Unused
* @return void
*/
public function addBasePath($path, $prefix = 'Zend_View')
{
return $this->setScriptPath($path);
}
/**
* 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);
}
/**
* 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, $value = NULL) {
return $this->_smarty->fetch($name);
}
public function display($name, $value = NULL) {
echo $this->_smarty->fetch($name);
}
}
?>
(4)修改application的配置文件,添加smarty部分的配置内容:
vim application.ini
[common]
application.directory = APPLICATION_PATH "/application"
application.dispatcher.catchException = TRUE
application.bootstrap = APPLICATION_PATH "/application/Bootstrap.php"
application.library = APPLICATION_PATH "/application/library"
application.baseUri = ''
;application.dispatcher.defaultModule = index
application.dispatcher.defaultController = index
application.dispatcher.defaultAction = index
;errors (see Bootstrap::initErrors)
application.showErrors=0
[smarty : common]
application.view.ext="tpl" ;;设置视图文件的后缀为 tpl
;smarty.left_delimiter = "{{" ;设置模板提取值时候的"{"情况
;smarty.right_delimiter = "}}" ;
smarty.template_dir = APPLICATION_PATH "/application/views/"
smarty.compile_dir = APPLICATION_PATH "/application/views/templates_c/"
smarty.cache_dir = APPLICATION_PATH "/application/views/templates_d/"
;smarty.caching = 0;
;smarty.cache_lifetime = 600;
[product : smarty]
(5)基于Yaf + Smarty的一个简单的MVC例子:
控制器(controllers):在controllers目录下添加Smarty.php控制器文件
vim Smarty.php
getView()->assign("content", "Hello Hadoop! Welcome to Beijing!
");
/*指定template_dir目录下的模板*/
$this->getView()->display('smarty.tpl');
/*false为禁止显示默认模板 return false表示显示display指定的模板*/
//return false;
}
}
?>
视图(views): 通过自定义的视图引擎(Smarty)渲染web页面,在views目录下添加smarty目录,然后分别在smarty目录下添加smarty.tpl文件和在views目录下直接添加smarty.tpl文件(测试display方法用)
vim smarty.tpl
A Smarty Adapter Example
{$content}
(6)测试:在浏览器中输入:http://172.16.2.33/smarty/smarty,结果显示:
Hello Hadoop! Welcome to Beijing! //默认的视图文件的存放目录,即views/smarty/目录下
Hello Hadoop! Welcome to Beijing! //通过display指定的视图文件存放目录