PHP.MVC的模板标签系统(四)

页面布局

在这个单元中我们将看到如何使用模板标签系统构造一个标准的模板页面。这个例子我们使用了一个简单的HTML页面布局,请看下图:

页面布局

这个页面有多个标准单元组成,就像页面设计者和开发者熟悉的那样.这个页面的主体由3个包含的单元组成:页眉,页内容主体和页脚.我们现在就看看这些单元并且了解如何使用模板标签系统来实现.

页主体

下面的代码单元显示的是主体:
The Page Body Layout
1
<@ saleMonth = data.getValueBean('SALE_MONTH') @>
<@ saleTitle = data.getValueBean('SALE_TITLE') @>
<@ dealHeading = data.getValueBean('DEAL_HEADING') @>
<@ salesAreaID = "Central District" @>

 1<html>
 2<head>
 3<link href="./style/pageStyles.css" rel="stylesheet" type="text/css"/>
 4<title>   
 52 &lt;@ =viewConfig.getAppTitle @&gt;   
 6</title>
 7</head>
 8<body>
 9<table class="pageLayoutTable">
10<!-- PAGE HEADER -->
11<tr>
12<td class="pageHeader">
13<!-- including the page header component -->
14<!-- The base template base directory is "./tpl" -->   
153 &lt;@ include 'pageHeader.ssp' @&gt;   
16</td>
17</tr>
18<!-- PAGE CONTENTS -->
19<tr valign="top">
20<td class="pageContent">
21<!-- including the page contents component -->   
224 &lt;@ include 'sale/pageContent.ssp' @&gt;   
23</td>
24</tr>
25<!-- PAGE FOOTER -->
26<tr>
27<td class="pageFooter">
28<!-- including the page footer omponent -->   
295 &lt;@ include 'pageFooter.ssp' @&gt;   
30</td>
31</tr>
32</table>
33</body>
34</html>

1:页声明
第一个有趣的条目是页顶部的页声明(1).我们在页面开始声明了这些变量,因此这些变量将能在下面的页面和像页眉那样的包含页所使用.
2:页标题
下一步我们使用表达式来初始化页面标题(2).这个值能够从配置文件中view-resources元素利用ViewResourcesConfig->getAppTitle来得到:

 1<view-resources ...="" <="" apptitle="Flash Jacks' Sleek Tab Site" view-resources="">   
 23:页眉   
 3页眉是下一个有趣的条目(3).在这里我们使用包含指令来插入页眉模板文件到页主体中.我们将在下一个子单元中来看一看页眉.   
 4我们仅仅使用了页面直接去读取页眉,不论页的组件存储在哪里.这是一个好机会来介绍模板标签系统的目录设置.默认情况下,模板目录布局如下所示(注意这些路径相对于我们的应用程序):   
 5The Default PhpMVC_Tags Template Directory Layout Paths (relative)   
 6The Template Files './WEB-INF/tpl'   
 7The Compiled Template Files './WEB-INF/tpl_C'   
 8如果需要的话我们可以在配置文件的view-resources结点来重新定义他们,就像这样:   
 9<view-resources ...="" <="" tpldir="./WEB-INF/tpl-admin" tpldirc="./WEB-INF/tpl_admin_C" view-resources="">   
104:页内容主体   
11这是另外一个包含指令被用来插入模板文件(4)到主体中.注意包含的文件位于模板目录的sales子目录中:   
12"./WEB-INF/tpl/sale/pageContent.ssp"   
135:页脚   
14又是一个包含指令,就像页眉一样. 
15
16页眉单元 
17
18在这个例子中页眉模板文件('pageHeader.ssp')只是一个简单的单元,就像这样:   
19<!-- Page Header -->
20<span>   
21&lt;@ =viewConfig.getAppTitle @&gt;   
22</span>   
23当主体页面(包括包含的页面)被编译的时候,页眉的表达式被转换成下面这样:   
24<!-- Page Header -->
25<span>   

print $viewConfig-&gt;getAppTitle();

 1</span>   
 2被编译的页面被存储在编译模板目录中,就像上面所说的,默认的编译模板目录是:   
 3'./WEB-INF/tpl_C' 
 4
 5页内容主体单元 
 6
 7页内容主体模板文件有一点复杂.文件('sale/pageContent.ssp')内容显示如下:   
 8...   
 91   
10&lt;@ item1=data-&gt;getValueBean("ITEM_1") @&gt;   
11&lt;@ products=data-&gt;getValueBean("PRODUCTS_ARRAY") @&gt;
12
132   
14<h4>&lt;@=dealHeading @&gt; &lt;@=saleMonth @&gt;</h4>
15
163   
17<b>Clearance deals</b>
18<table class="productsTable">
19<tr>
20<td class="prodItemDesc">   
21&lt;@ =item1.getName @&gt;   
22</td>
23<td class="prodItemValue">   
24&lt;@ =item1.getCost @&gt;   
25</td>
26</tr>
27</table>
28
294   
30<b>Todays specials</b>
31<table class="productsTable">   

foreach($products as $item) {

1<tr>
2<td class="prodItemDesc">   
3&lt;@ =item.getName @&gt;   
4</td>
5<td class="prodItemValue">   
6&lt;@ =item.getCost @&gt;   
7</td>
8</tr>   

}

 1</table>
 2<b>Our Staff at Your Service</b>   
 3...   
 45   
 5<table class="productsTable">
 6<tr>
 7<td class="prodItemDesc">
 8<b>Area Manager: </b>
 9</td>
10<td class="prodItemDesc">   
11&lt;@ =viewConfig.getAreaManager @&gt;   
12</td>
13</tr>   
14...   
15</table>   
161:一些更多的声明   
17在页面顶部所显示的额外声明(1)能让我们声明页变量以便下面能够使用.在内容被处理之后,这些声明将在编译后像下面这样显示:   

$item1=$data-&gt;getValueBean("ITEM_1");

1...   

$products=$data-&gt;getValueBean("PRODUCTS_ARRAY");

12:使用表达式来显示内容单元标题   
2现在我们使用两个表达式(2)来显示内容单元的标题.注意我们声明这些变量是"全局"变量在主页面的顶部.处理完后,表达式将转换这些代码,就像这样:   

print $dealHeading; print $saleMonth;

 1当页面被显示到用户的浏览器中,内容单元的标题看起来就像这样:   
 2Jack's Super Deals for : May 2010.   
 33:使用表达式来显示一些数据条目   
 4现在我们能显示一些实际的数据(3).在这个页内容主体单元中我们访问一些在PhpMVCTabAction类的ActionObject中的产品条目数据.一个简化版的PhpMVCTabAction类在下面展示:   
 5class PhpMVCTabAction extends Action {   
 6...   
 7function execute($mapping, $form, &amp;$request, &amp;$response) {   
 8// Our value bean container   
 9$valueBeans =&amp; new ValueBeans(); 
10
11// Define some strings we need on our View template page   
12// These could be defined globally in the phpmvc-config.xml file.   
13// See: ExtendedController example.   
14$appTitle = "Flash Jack's Include Page";   
15$saleMonth = "May 2010";   
16$saleTitle = "Flash Jack's Super Sale";   
17$dealHeading = "Jack's Super Deals for :";   
18... 
19
20// Save the string variables to our Value object   
21$valueBeans-&gt;addValueBean('APP_TITLE' , $appTitle);   
22$valueBeans-&gt;addValueBean('SALE_MONTH' , $saleMonth);   
23$valueBeans-&gt;addValueBean('SALE_TITLE' , $saleTitle);   
24$valueBeans-&gt;addValueBean('DEAL_HEADING' , $dealHeading);   
25... 
26
27// Some float values we could receive from a database query   
28// Note: The prices are formatted in the Products class constructor.   
29// Eg: "$ n,nnn.nn"   
30$price1 = 125.00;   
31... 
32
33// Setup some clearance deals (individual object instances):   
34// Note: The Product class file was included in our local prepend.php file   
35$item1 = new Product('Super Duper', $price1);   
36...   
37$valueBeans-&gt;addValueBean('ITEM_1', $item1);   
38... 
39
40// Todays specials (array of object instances)   
41$products = array();   
42$products[] = new Product('Gooses Bridle', $price3);   
43...   
44$valueBeans-&gt;addValueBean('PRODUCTS_ARRAY', $products); 
45
46// Our staff   
47$staff1 =&amp; new Staff('Bruce', 'Sales', 'Karate');   
48...   
49$valueBeans-&gt;addValueBean('STAFF_1', $staff1);   
50... 
51
52// Save the Value object   
53$this-&gt;saveValueObject($request, $valueBeans);   
54在上面的代码中,我们能看到$item1被创建并被保存成ActionObject的valueBeans条目.Bean数据条目现在能在模板页面中被重新获得:   
55&lt;@ item1=data-&gt;getValueBean("ITEM_1") @&gt;   
56我们可以像下面那样显示条目的值:   
57&lt;@ =item1.getName @&gt;   
58...   
59&lt;@ =item1.getCost @&gt;   
604:显示数组   
61我们也可以直接使用一些PHP代码在我们的模板页上.在这个分离的MVC模式中,我们应该仅在这里编写代码去操纵这些通过ActionObject和ViewResourcesConfig实例(可能我们的自定义Bean也可以)提供的数据.在上面的也内容单元('sale/pageContent.ssp')中,我们使用一个PHP的foreach语法(4)来循环读取$products数组.我们能在上面的PhpMVCTabAction类中看到$products数组被创建并被保存在ActionObject中,就和上面的$item1 Bean相似.在foreach循环中我们能使用表达式来显示产品数据:   

foreach($products as $item) {

1<tr>
2<td class="prodItemDesc">   
3&lt;@ =item.getName @&gt;   
4</td>
5<td class="prodItemValue">   
6&lt;@ =item.getCost @&gt;   
7</td>
8</tr>   

}

 15:显示ViewResourcesConfig属性   
 2最后我们从view-resources元素所定义的ViewResourcesConfig属性来显示"Area Manager"(5)在我们的内容页:   
 3<view-resources ...="" apptitle="Flash Jacks' Sleek Tab Site" classname="MyViewResourcesConfig">
 4<!-- We can set some properties on our custom ViewResourcesConfig class -->
 5<set-property property="areaManager" value="Joe J. Blogs Esq."></set-property>
 6</view-resources>   
 7但是注意在这个例子中我们使用了一个继承ViewResourcesConfig类的对象(MyViewResourcesConfig)来设置一些自定义的属性.我们定义了一个扩展ViewResourcesConfig类的对象,在配置文件里使用className="MyViewResourcesConfig"属性,并且MyViewResourcesConfig类定义在文件"MyViewResourcesConfig.php"中.MyViewResourcesConfig类(classes/MyViewResourcesConfig.php)实现了setter/getter方法去处理自定义属性("areaManager"),这个属性我们在view-resources结点中定义:   
 8class MyViewResourcesConfig extends ViewResourcesConfig { 
 9
10// ----- Properties ----------------------------------------------------- // 
11
12var $areaManager = ''; 
13
14function getAreaManager() {   
15return $this-&gt;areaManager;   
16} 
17
18function setAreaManager($areaManager) {   
19$this-&gt;areaManager = $areaManager;   
20}   
21我们现在能使用表达式在我们的页面上实现"Area Manager"了:   
22&lt;@ =viewConfig.getAreaManager @&gt;   
23注意:在真实的应用程序中数据能从关系型数据库中得到. 
24
25页脚单元 
26
27页脚单元和上面讨论过的页眉单元的处理相类似.页脚模板文件('tpl/pageFooter.ssp')就像这样:   
28<!-- Page Footer -->
29<span>   
30&lt;@ =viewConfig.getCopyright @&gt;   
31</span>   
32当主体页面(包括包含的页面)被编译,在页脚中的表达式被转换成下面这样:   
33<!-- Page Footer -->
34<span>   

print $viewConfig-&gt;getCopyright();

1</span>   
2编译的页眉页面被存储在编译模板目录.默认的编译模板目录是:   
3'./WEB-INF/tpl_C'</view-resources></view-resources>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus