Yaf 结合用户自定义的视图(模板)引擎Smarty(Yaf + Smarty)
来源:互联网
作者:佚名
时间:2015-08-06 07:55
对完成某个任务进行计时可使用progress_timer类,这个类对象在退出作用范围后,会输出对象创建后过去的时间,可申请多个类对象,这样可对多个任务进行统计。#inc
Yaf 结合用户自定义的视图(模板)引擎Smarty(Yaf + Smarty)
分类:
php开发
yafsmarty
(1)入口文件:/public/index.php:
define("DS", '/');
define('APPLICATION_PATH', dirname(__FILE__).DS.'..'.DS);//指向public上一级的目录 ../
$application = new Yaf_Application( APPLICATION_PATH . "/conf/application.ini");
$application->bootstrap()->run();
?>
(2)在引导程序
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_Adapter
首先下载
vim Adapter.php
/*确保Smarty.class.php在Smarty/libs/下*/
Yaf_Loader::import( "Smarty/libs/Smarty.class.php"); /*基类目录为library*/
class Smarty_Adapter implements Yaf_View_Interface /*Smarty_Adapter类为yaf与smarty之间的适配器*/
{
/**
* Smarty object
* @var Smarty
*/
public $_smarty;
/**
* Constructor
*
* @param string $tmplPath
* @param array $extraParams
* @return void
*/
public function __construct($tmplPath = null, $extraParams = array()) {
$this->_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)修改
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