本文記錄Yaf與smarty視圖引擎結果的過程,歡迎交流。
(1)下載smarty視圖引擎,本筆記採用的版本爲smarty-3.1.32
下載地址:https://github.com/smarty-php/smarty/releases/tag/v3.1.32
windows下載zip版本即可。
(2)將smarty框架添加到Yaf項目中
Yaf默認的視圖引擎:
yaf框架默認的視圖引擎爲Yaf_View_Simple
這是Yaf內建的一個模板引擎,是個簡單而快速的模板引擎,只支持PHP腳本
添加smarty視圖引擎步驟:
1.解壓smarty的zip壓縮文件至Yaf項目的application/library目錄下;
2、目錄結構如下:
|- application
|+ controllers
|+ views
|+ models
|+ modules
|+ plugins
|+ library
|+ smarty-3.1.32 //裏面的文件原樣保留即可,略
|+ cache
bootstrap.php
| +conf
| +public
3、開始進行Yaf與smarty整合。
(3)入口文件:/public/index.php:
編輯內容如下:
<?php
define("DS", '/');
define('APPLICATION_PATH', dirname(__FILE__).DS.'..'.DS);//指向public上一級的目錄 ../
$application = new Yaf_Application( APPLICATION_PATH . "/conf/application.ini");
$application->bootstrap()->run();
?>
(4)在引導程序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);
}
//其他定義忽略......
//初始化smarty視圖引擎
public function _initSmarty(Yaf_Dispatcher $dispatcher) {
Yaf_Loader::import( APP_PATH ."/application/library/smarty-3.1.32/libs/Adapter.php");
$smarty = new Smarty_Adapter(null, Yaf_Application::app()->getConfig()->smarty);
Yaf_Dispatcher::getInstance()->setView($smarty);
}
//關閉框架默認的自動渲染效果
public function _initView( Yaf_Dispatcher $dispatcher )
{
Yaf_Dispatcher::getInstance()->disableView(); //關閉其自動渲染
}
}
(5)添加Smarty的適配器即Smarty_Adapter類
目的:使Yaf和Smarty之間能進行適配
把你需要view做的功能實現在Smarty_Adapter類裏面實現,yaf通過他們來操作smarty的特性。
在上述項目目錄:application/library/smarty-3.1.32/libs中添加Adapter.php文件,文件內容編輯如下:
<?php
/**
* Created by PhpStorm.
* User: youth
* Date: 2018-05-03
* Time: 17:59
*/
Yaf_Loader::import( APP_PATH ."/application/library/smarty-3.1.32/libs/Smarty.class.php");
Yaf_Loader::import( APP_PATH . "/application/library/smarty-3.1.32/libs/sysplugins/smarty_internal_templatecompilerbase.php");
Yaf_Loader::import( APP_PATH . "/application/library/smarty-3.1.32/libs/sysplugins/smarty_internal_templatelexer.php");
Yaf_Loader::import( APP_PATH . "/application/library/smarty-3.1.32/libs/sysplugins/smarty_internal_templateparser.php");
Yaf_Loader::import( APP_PATH . "/application/library/smarty-3.1.32/libs/sysplugins/smarty_internal_compilebase.php");
Yaf_Loader::import( APP_PATH . "/application/library/smarty-3.1.32/libs/sysplugins/smarty_internal_write_file.php");
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);
}
}
注意:文件引用需要注意相對路徑和絕對路徑的問題,如果對相對路徑不確定,建議直接使用絕對路徑引入即可。
(6)修改application的配置文件,添加smarty部分的配置內容
編輯文件:conf/application.ini文件內容如下:
[product]
;直接寫自己定義的常量
;application.debug=1
application.directory=APP_PATH "/application/"
application.bootstrap=APP_PATH "/bootstrap.php"
;application.dispatcher.defaultModule="index"
;application.dispatcher.defaultController="index"
;application.dispatcher.defaultAction="index"
;application.dispatcher.throwException=1
application.modules="index,admin"
;application.module.dir="modules"
;application.module.config="setting"
;application.view.ext="phtml"
;application.view.ext="html"
smarty.left_delimiter= "<!--{"
smarty.right_delimiter= "}-->"
smarty.template_dir= APP_PATH "/application/views/"
smarty.compile_dir= APP_PATH "/application/cache/compile"
smarty.cache_dir= APP_PATH "/application/cache/"
(7)基於Yaf + Smarty的一個簡單的MVC例子:
控制器(controllers):在controllers目錄下添加Index.php控制器文件
<?php
class SmartyController extends Yaf_Controller_Abstract
{
public function smartyAction()
{
/*默認template_dir目錄下two/two.tpl*/
$this->getView()->assign("content", "Hello Hadoop! Welcome to Beijing!<br/>");
/*指定template_dir目錄下的模板*/
$this->getView()->display('/index/index.tpl');
/*false爲禁止顯示默認模板 return false表示顯示display指定的模板*/
//return false;
}
}
?>
視圖(views): 通過自定義的視圖引擎(Smarty)渲染web頁面,在views目錄下添加index目錄,然後目錄下添加index.tpl文件
<html>
<head>
<title>A Smarty Adapter Example
</title>
</head>
<body>
<!--{$content}-->
</body>
</html>
即可輸出內容。
(8)視圖模板渲染(待續)
可以實現前端代碼模塊化,避免相同的版塊在每個頁面都進行編寫,修改起來需要到每個頁面進行修改,這樣很不方便。
使用smarty框架的模板繼承可以解決這個效率問題。