php yaf smarty,Yaf 结合用户自定义的视图(模板)引擎Smarty(Yaf + Smarty)
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
總結
以上是生活随笔為你收集整理的php yaf smarty,Yaf 结合用户自定义的视图(模板)引擎Smarty(Yaf + Smarty)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用一根网线连接树莓派树莓派有线网络连接
- 下一篇: php 百度云 上传,求个PHP版百度云