本文记录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框架的模板继承可以解决这个效率问题。
版权声明:本文为Pencil1312原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。