这几天刚刚开始关注
PHP的各种
开发框架,ZF第一感觉比较不错,只可惜本人E文太差,加上只支持PHP5,只能以后再研究了;
CAKEPHP好像也不错,
中文的
教程和
手册都有,但是看到FleaPHP后,还是觉得支持国产好,手册、
指南和注释都是中文,哈哈……爽!
本人之前一直用PHP+PHPLIB来做
项目,
PHPLIB来做小项目还是挺爽的,呵呵,所以看到FleaPHP没有默认支持PHPLIB还是有点遗憾,虽然问过老大说可以像之前一样用。今天闲着没事就想着自己来增加对PHPLIB的支持,简单测试一下,还真可以了,呵呵,下面就公布一下我的成果,但是水平有限,加上对FleaPHP还是略懂一二,如果有不当之处,还希望各位多多指教!
首先,在FLEA\View目录下增加
Template.php文件,里面代码如下:
复制内容到剪贴板
代码:
/**
* 定义 FLEA_View_Template 类
*
* @copyright Copyright (c) 2005 - 2006 FleaPHP.org (www.fleaphp.org)
* @author
* @package Core
* @version $Id: Template.php 640 2007-03-17
*/
// {{{ includes
if (!class_exists('Template')) {
$viewConfig = get_app_inf('viewConfig');
if (!isset($viewConfig['PHPLIB_dir'])) {
load_class('FLEA_View_Exception_NotConfigurationTemplate');
__THROW(new FLEA_View_Exception_NotConfigurationTemplate());
}
$filename = $viewConfig['PHPLIB_dir'] . '/template.inc.php';
if (!is_readable($filename)) {
load_class('FLEA_View_Exception_InitTemplateFailed');
__THROW(new FLEA_View_Exception_InitTemplateFailed($filename));
}
require($filename);
}
// }}}
/**
* FLEA_View_Template 提供了对 Template 模板引擎的支持
*
* @package Core
* @author
* @version 1.0
*/
class FLEA_View_Template extends Template
{
/**
* 构造函数
*
* @return FLEA_View_Template
*/
function FLEA_View_Template() {
log_message('Construction FLEA_View_Template', 'debug');
$viewConfig = get_app_inf('viewConfig');
if (!is_array($viewConfig)) {
return;
}
parent::Template($viewConfig['template_dir'], $viewConfig['view_unknowns']);
}
/**
* 对Template原来的set_var()方法进行重新包装以便跟其他模板的方法兼容
*
* @param array $date
*/
function assign($date) {
parent::set_var($date);
}
/**
* 输出指定模版的内容
*
* @param string $tpl
*/
function display($tpl) {
parent::parse("out", $tpl);
parent::p("out");
}
}然后,就是在
应用程序入口
文件定义设置使用:
复制内容到剪贴板
代码:
$appInf = array(
'view' => 'FLEA_View_Template',
'viewConfig' => array(
'PHPLIB_dir' => APP_DIR.'/LIB/vendors',//template.inc.php 该模板核心类文件所在目录
'template_dir' => APP_DIR.'/template/',//网页模版文件所在目录
'view_unknowns' => 'keep',
),
);还有,需要在FLEA\View\Exception中参照里面的例子写NotConfigurationTemplate.php和InitSmartTemplateFailed.php这两个导入类文件时的异常处理文件,里面的具体代码就不写了。
最后来个测试例子(随便拿老大的一个列子改的):
复制内容到剪贴板
代码:
function actionIndex() {
/**
* 用 get_singleton() 获取 Model 的对象实例。
*
* 该函数返回指定类的唯一一个实例。由于 get_singleton() 会尝试自动载入类
* 定义文件,所以使用非常方便。
*/
$modelSayName =& get_singleton('Model_SayName');
/* @var $modelSayName Model_SayName */
/**
* 上面那行看上去奇怪的注释,是帮助诸如 Zend Development Environment 和
* Eclipse PHP IDE 这样的编辑器识别 $modelSayName 变量的正确类型。
*/
/**
* 调用 Model 获取数据
*/
$name = $modelSayName->say();
/**
* 在 FleaPHP,通常不需要直接获取视图对象。
*
* 而是调用 FLEA_Controller_Action::_executeView() 方法,直接输出视图。
*
* _executeView() 方法的第一个参数是视图的名字(大多数时候是视图的文件名,例如对于 Smarty
* 来说,就是模版文件名),第二个参数是要传递给视图的变量(必须是一个数组)。
*/
$viewData = array(
'name' => $name,
);
$view =& $this->_getView();
$view->set_file("index","tpl_index.htm");
$this->_executeView('index', $viewData);
}