认真研究了
www.pear.net 上给出的例子,觉得它把elements[]放在controller类的一个output()函数中声明并赋值,破坏了类的OO封装特性,data最好应该限制只能作为class的(私有)成员,而不是在成员函数中冒出来。那个例子应该只是为了更方便的说明问题吧,我想。
我重新写了个controller通用模型,逻辑比较清晰,OO性能好,骨干结构如下:
复制内容到剪贴板
代码:
class page_data {} //定义数据类
$data = new page_data(……); //构造函数调用数据层操纵接口,实例化数据类
for(……) //定义elements数组,用于表示层的所有form的tag元素替代
{
$elements['xxxx']=……;
}
$output = new HTML_Template_Flexy(); //实例化controller类(逻辑层)
$output->compile($data->template); //对表示层的html模板,做tag -> php codes 的替代
$output->outputObject($data,$elements); //加载表示层的模板,执行php codes部分,输出html代码以下是调试通过的例子:
controller代码:
复制内容到剪贴板
代码:
<?php
require_once 'PEAR.php';
/* configure the application - probably done elsewhere */
require_once 'HTML/Template/Flexy/Element.php';
require_once 'HTML/Template/Flexy.php';
$options = &PEAR::getStaticProperty('HTML_Template_Flexy','options');
$config = parse_ini_file('example.ini',TRUE);
$options = $config['HTML_Template_Flexy'];
/* the page controller class */
class page_data
{
var $template = "home.html"; // name of template
var $title;
var $sub; // this relates to {title};
var $functions = array(); // this relates to {numbers} , used with foreach
var $anObject;
var $formObject;
var $element = array(); // this is where the elements are stored
var $a = array(
"dog" => "cat",
"fire" => "water"
);
var $b = array('a','b','c');
var $c = array(0 => 'tommy',1 => 'Yeen',2 => 'Joe');
/* start section - deals with posts, get variables etc.*/
function page_render()
{
$this->prepare();
$this->output();
}
function page_data()
{
// the title
$this->title = "我的个人空间<>";
$this->sub = "tommy's home";
// store an object.
$this->anObject = new StdClass;
// assign a value to a member.
$this->anObject->member = 'Object Member';
// store an object.
$this->formObject = new StdClass;
// assign a value to a member.
$this->formObject->text = "input here";
for ($i = 1;$i< 5;$i++) {
$this->fuctions[$i] = "功能 $i";
}
}
/* output section - probably best to put this in the default_controller class */
function output() {
}
function someMethod() {
echo "<b>Hello >> From Method member</b>";
}
}
//make the data object
$data = new page_data;
// create HTML Elements for the form element.
$elements['input'] = new HTML_Template_Flexy_Element;
$elements['input']->setValue('请在此输入要搜索的文字');
$elements['text'] = new HTML_Template_Flexy_Element;
$elements['text']->setValue('input here');
$elements['subtitle'] = new HTML_Template_Flexy_Element;
$elements['subtitle']->setValue($data->sub);
//make the controller object
$output = new HTML_Template_Flexy();
$output->compile($data->template);
$output->outputObject($data,$elements);
?>template文件:
复制内容到剪贴板
代码:
<html>
<head>
<title>{title}</title>
</head>
<body>
<H1>{title}{title:h}{title:u}</H1>
<form name="form">
<table>
<tr>
<td>
Input Box: <input name="input"><input name="subtitle">
</td>
</tr>
<tr flexy:foreach="functions,function,string">
<td>
<a href="mypage.html?id=%7Bnumber%7D">{string}</a>
</td>
</tr>
</table>
</form>
<form name="form from object" flexyobject="formObject">
<TEXTAREA name="text">321sdftw43twret43wt</TEXTAREA>
</form>
this is function 2 : {functions[2]}<br>
This is a/an {anObject.member}<br>
This is from someMethod() -- {someMethod()}<br>
This is from someMethod():h -- {someMethod():h}<br>
This is from someMethod():u -- {someMethod():u}<br>
<table border>
<tr flexy:foreach="a,k,v">
<td border>k is {k}, and v is {v}</td>
</tr>
</table>
<table border>
<tr flexy:foreach="b,v">
<td border>v is {v}</td>
</tr>
</table>
<table border>
<tr flexy:foreach="c,x,y">
<td border>{if:x}Hello,No.{x} is {y}{else:}I'm zero.{end:}</td>
</tr>
</table>
</body>
</html>