您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/12/8 18:29:02发布18次查看
页面布局
    在这个单元中我们将看到如何使用模板标签系统构造一个标准的模板页面。这个例子我们使用了一个简单的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" @>
<html>
<head>
   <link rel='stylesheet' type='text/css' href="./style/pagestyles.css"/>
   <title>
2     <@ =viewconfig.getapptitle @>
   </title>
</head>
<body>
<table class='pagelayouttable'>
   <!-- page header -->
   <tr>
      <td class='pageheader'>
         <!-- including the page header component -->
         <!-- the base template base directory is "./tpl" -->
3        <@ include 'pageheader.ssp' @>
      </td>
   </tr>
   <!-- page contents -->
   <tr valign='top'>
      <td class='pagecontent'>
         <!-- including the page contents component -->
4        <@ include 'sale/pagecontent.ssp' @>
      </td>
   </tr>
   <!-- page footer -->
   <tr>
      <td class='pagefooter'>
         <!-- including the page footer omponent -->
5        <@ include 'pagefooter.ssp' @>
      </td>
   </tr>
</table>
</body>
</html>
1:页声明
    第一个有趣的条目是页顶部的页声明(1).我们在页面开始声明了这些变量,因此这些变量将能在下面的页面和像页眉那样的包含页所使用.
2:页标题
    下一步我们使用表达式来初始化页面标题(2).这个值能够从配置文件中view-resources元素利用viewresourcesconfig->getapptitle来得到:
<view-resources
apptitle = "flash jacks' sleek tab site"
...
</view-resources>
3:页眉
    页眉是下一个有趣的条目(3).在这里我们使用包含指令来插入页眉模板文件到页主体中.我们将在下一个子单元中来看一看页眉.
    我们仅仅使用了页面直接去读取页眉,不论页的组件存储在哪里.这是一个好机会来介绍模板标签系统的目录设置.默认情况下,模板目录布局如下所示(注意这些路径相对于我们的应用程序):
the default phpmvc_tags template directory layout paths (relative)  
the template files  './web-inf/tpl'  
the compiled template files  './web-inf/tpl_c'  
    如果需要的话我们可以在配置文件的view-resources结点来重新定义他们,就像这样:
<view-resources
...
tpldir = "./web-inf/tpl-admin"
tpldirc = "./web-inf/tpl_admin_c"
...
</view-resources>
4:页内容主体
    这是另外一个包含指令被用来插入模板文件(4)到主体中.注意包含的文件位于模板目录的sales子目录中:
./web-inf/tpl/sale/pagecontent.ssp
5:页脚
    又是一个包含指令,就像页眉一样.
页眉单元
    在这个例子中页眉模板文件('pageheader.ssp')只是一个简单的单元,就像这样:
<!-- page header -->
<span>
   <@ =viewconfig.getapptitle @>
</span>
    当主体页面(包括包含的页面)被编译的时候,页眉的表达式被转换成下面这样:
<!-- page header -->
<span>
   <?php print $viewconfig->getapptitle(); ?>
</span>
    被编译的页面被存储在编译模板目录中,就像上面所说的,默认的编译模板目录是:
'./web-inf/tpl_c'
页内容主体单元
    页内容主体模板文件有一点复杂.文件('sale/pagecontent.ssp')内容显示如下:
...
1
<@ item1=data->getvaluebean(item_1) @>
<@ products=data->getvaluebean(products_array) @>
2
<h4><@=dealheading @> <@=salemonth @></h4>
3
<b>clearance deals</b>
<table class='productstable'>
   <tr>
      <td class='proditemdesc'>
         <@ =item1.getname @>
      </td>
      <td class='proditemvalue'>
         <@ =item1.getcost @>
      </td>
   </tr>
</table>
4
<b>todays specials</b>
<table class='productstable'>
<?php foreach($products as $item) { ?>
  <tr> 
    <td class='proditemdesc'>
         <@ =item.getname @>
    </td>
    <td class='proditemvalue'>
         <@ =item.getcost @>
    </td>
  </tr>
<?php } ?>
</table>
<b>our staff at your service</b>
...
5
<table class='productstable'>
  <tr>
    <td class='proditemdesc'>
      <b>area manager: </b>
    </td>
    <td class='proditemdesc'>
      <@ =viewconfig.getareamanager @>
    </td>
  </tr>
  ...
</table>
1:一些更多的声明
    在页面顶部所显示的额外声明(1)能让我们声明页变量以便下面能够使用.在内容被处理之后,这些声明将在编译后像下面这样显示:
<?php $item1=$data->getvaluebean(item_1); ?>
...
<?php $products=$data->getvaluebean(products_array); ?>
2:使用表达式来显示内容单元标题
    现在我们使用两个表达式(2)来显示内容单元的标题.注意我们声明这些变量是全局变量在主页面的顶部.处理完后,表达式将转换这些代码,就像这样:
<?php print $dealheading; ?> <?php print $salemonth; ?>
    当页面被显示到用户的浏览器中,内容单元的标题看起来就像这样:
jack's super deals for : may 2010.
3:使用表达式来显示一些数据条目
    现在我们能显示一些实际的数据(3).在这个页内容主体单元中我们访问一些在phpmvctabaction类的actionobject中的产品条目数据.一个简化版的phpmvctabaction类在下面展示:
class phpmvctabaction extends action { 
   ...
   function execute($mapping, $form, &$request, &$response) {
      // our value bean container
      $valuebeans =& new valuebeans();
      // define some strings we need on our view template page
      // these could be defined globally in the phpmvc-config.xml file.
      // see: extendedcontroller example.
      $apptitle      = flash jack's include page;
      $salemonth     = may 2010;
      $saletitle     = flash jack's super sale;
      $dealheading   = jack's super deals for :;
      ...
      // save the string variables to our value object
      $valuebeans->addvaluebean('app_title'     , $apptitle);
      $valuebeans->addvaluebean('sale_month'    , $salemonth);
      $valuebeans->addvaluebean('sale_title'    , $saletitle);
      $valuebeans->addvaluebean('deal_heading'  , $dealheading);
      ...
      // some float values we could receive from a database query
      // note: the prices are formatted in the products class constructor. 
      // eg: $ n,nnn.nn
      $price1 =  125.00;
      ...
      // setup some clearance deals (individual object instances):
      // note: the product class file was included in our local prepend.php file
      $item1 = new product('super duper', $price1);
      ...
      $valuebeans->addvaluebean('item_1', $item1);
      ...
      // todays specials (array of object instances)
      $products = array();
      $products[] = new product('gooses bridle', $price3);
      ...
      $valuebeans->addvaluebean('products_array', $products);
      // our staff
      $staff1 =& new staff('bruce', 'sales', 'karate');
      ...
      $valuebeans->addvaluebean('staff_1', $staff1);
      ...
      // save the value object
      $this->savevalueobject($request, $valuebeans);
    在上面的代码中,我们能看到$item1被创建并被保存成actionobject的valuebeans条目.bean数据条目现在能在模板页面中被重新获得:
<@ item1=data->getvaluebean(item_1) @>
    我们可以像下面那样显示条目的值:
<@ =item1.getname @>
...
<@ =item1.getcost @>
4:显示数组
    我们也可以直接使用一些php代码在我们的模板页上.在这个分离的mvc模式中,我们应该仅在这里编写代码去操纵这些通过actionobject和viewresourcesconfig实例(可能我们的自定义bean也可以)提供的数据.在上面的也内容单元('sale/pagecontent.ssp')中,我们使用一个php的foreach语法(4)来循环读取$products数组.我们能在上面的phpmvctabaction类中看到$products数组被创建并被保存在actionobject中,就和上面的$item1 bean相似.在foreach循环中我们能使用表达式来显示产品数据:
<?php foreach($products as $item) { ?>
   <tr> 
      <td class='proditemdesc'>
         <@ =item.getname @>
      </td>
      <td class='proditemvalue'>
         <@ =item.getcost @>
       </td>
   </tr>
<?php } ?>
5:显示viewresourcesconfig属性
    最后我们从view-resources元素所定义的viewresourcesconfig属性来显示area manager(5)在我们的内容页:
<view-resources
apptitle = "flash jacks' sleek tab site"
...
classname = "myviewresourcesconfig">
       <!-- we can set some properties on our custom viewresourcesconfig class -->
       <set-property property="areamanager" value="joe j. blogs esq."/>
  </view-resources>
    但是注意在这个例子中我们使用了一个继承viewresourcesconfig类的对象(myviewresourcesconfig)来设置一些自定义的属性.我们定义了一个扩展viewresourcesconfig类的对象,在配置文件里使用classname=myviewresourcesconfig属性,并且myviewresourcesconfig类定义在文件myviewresourcesconfig.php中.myviewresourcesconfig类(classes/myviewresourcesconfig.php)实现了setter/getter方法去处理自定义属性(areamanager),这个属性我们在view-resources结点中定义:
class myviewresourcesconfig extends viewresourcesconfig {
   // ----- properties ----------------------------------------------------- //
   var $areamanager = '';
   function getareamanager() {
      return $this->areamanager;
   }
   function setareamanager($areamanager) {
      $this->areamanager = $areamanager;
   }
    我们现在能使用表达式在我们的页面上实现area manager了:
<@ =viewconfig.getareamanager @>
    注意:在真实的应用程序中数据能从关系型数据库中得到.
页脚单元
    页脚单元和上面讨论过的页眉单元的处理相类似.页脚模板文件('tpl/pagefooter.ssp')就像这样:
<!-- page footer -->
<span>
  <@ =viewconfig.getcopyright @>
</span>
    当主体页面(包括包含的页面)被编译,在页脚中的表达式被转换成下面这样:
<!-- page footer -->
<span>
   <?php print $viewconfig->getcopyright(); ?>
</span> 
    编译的页眉页面被存储在编译模板目录.默认的编译模板目录是:
'./web-inf/tpl_c'
 以上就是php.mvc的模板标签系统(四)的内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product