Php.mvc Template Labeling System (iv) _php Foundation

Source: Internet
Author: User
Tags foreach php code php foreach php print
Page layout

In this unit we will see how to construct a standard template page using the template tag system. For this example we use a simple HTML page layout, please look at the following figure:

This page is composed of several standard units, as the page designer and developer are familiar with. The main body of this page consists of 3 elements: a header, a page content body, and a footer. Let's take a look at these units and learn how to use the template tag system to implement it.

Page body

The following code unit shows the principal:
The Page body Layout
1
<@ salemonth = Data.getvaluebean (' sale_month ') @>
<@ saletitle = Data.getvaluebean (' sale_title ') @>
<@ dealheading = Data.getvaluebean (' deal_heading ') @>
<@ salesareaid = "District" @>

<link rel= ' stylesheet ' type= ' text/css ' href= './style/pagestyles.css '/>
<title>
2 <@ =viewconfig.getapptitle @>
</title>
<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>

1: Page declaration
The first interesting entry is the page declaration at the top of the page (1). We declare these variables at the beginning of the page, so these variables will be available on the page below and on the Include page like the header.
2: page title
Next we use an expression to initialize the page title (2). This value can be obtained from the view-resources element in the configuration file using Viewresourcesconfig->getapptitle:
<view-resources
AppTitle = "Flash jacks ' Sleek tab Site"
...
</view-resources>
3: Header
The header is the next interesting entry (3). Here we use the Include directive to insert the header template file into the page body. We'll look at the header in the next child unit.
We just use the page to read the header directly, regardless of where the page's components are stored. This is a good opportunity to introduce the directory settings for the template label system. By default, the template directory layout looks like this (note that these paths are relative to our application):
The Default phpmvc_tags Template Directory Layout Paths (relative)
The Template Files './web-inf/tpl '
The Compiled Template Files './web-inf/tpl_c '
If necessary, we can redefine them at the view-resources node of the configuration file, like this:
<view-resources
...
Tpldir = "./web-inf/tpl-admin"
Tpldirc = "./web-inf/tpl_admin_c"
...
</view-resources>
4: Page content body
This is another containing instruction that is used to insert the template file (4) into the body. Note that the included file is in the sales subdirectory of the template directory:
"./web-inf/tpl/sale/pagecontent.ssp"
5: Footer
It's also a containing instruction, like a header.

Header Unit

In this example, the header template file (' PAGEHEADER.SSP ') is just a simple unit, like this:
<!--Page Header-->
<span>
<@ =viewconfig.getapptitle @>
</span>
When the main page (including the included pages) is compiled, the header expression is converted to the following:
<!--Page Header-->
<span>
<?php print $viewConfig->getapptitle ();?>
</span>
The compiled pages are stored in the compiled template directory, as mentioned above, and the default compilation template directory is:
'./web-inf/tpl_c '

Page Content Body Unit

The page content body template file is a bit complicated. The contents of the file (' SALE/PAGECONTENT.SSP ') are displayed as follows:
...
1
<@ Item1=data->getvaluebean ("item_1") @>
<@ Products=data->getvaluebean ("Products_array") @>

2

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>
&LT;TD class= ' Proditemdesc ' >
<b>area Manager: </b>
</td>
&LT;TD class= ' Proditemdesc ' >
<@ =viewconfig.getareamanager @>
</td>
</tr>
...
</table>
1: Some more statements
The extra statements shown at the top of the page (1) Let us declare the page variable so that it can be used below. After the content has been processed, the declarations are displayed as follows after compilation:
<?php $item 1= $data->getvaluebean ("item_1");?>
...
<?php $products = $data->getvaluebean ("Products_array");?>
2: Use an expression to display the Content cell title
Now we use two expressions (2) to display the title of the content unit. Note that we declare these variables to be "global" variables at the top of the main page. After processing, the expression converts the code, like this:
<?php print $dealHeading;?> <?php print $saleMonth;?>
When the page is displayed in the user's browser, the title of the content unit looks like this:
Jack ' s Super deals for:may 2010.
3: Use an expression to display some data entries
Now we can show some real data (3). In this page content body unit We visit some product entry data in the Actionobject of the Phpmvctabaction class. A simplified version of the Phpmvctabaction class is shown below:
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
    &nb Sp These could are 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"
$price 1 = 125.00;
...

Setup Some clearance deals (individual object instances):
Note:the Product class file is included in our local prepend.php file
$item 1 = new Product (' Super duper ', $price 1);
...
$valueBeans->addvaluebean (' item_1 ', $item 1);
...

Todays Specials (Array of object instances)
$products = Array ();
$products [] = new Product (' gooses bridle ', $price 3);
...
$valueBeans->addvaluebean (' Products_array ', $products);

Our Staff
$staff 1 =& New Staff (' Bruce ', ' Sales ', ' Karate ');
...
$valueBeans->addvaluebean (' staff_1 ', $staff 1);
...

Save the Value Object
$this->savevalueobject ($request, $valueBeans);
In the above code, we can see that $item1 is created and saved as a actionobject Valuebeans entry. Bean data entries can now be re-obtained in the template page:
<@ Item1=data->getvaluebean ("item_1") @>
We can display the value of the entry as follows:
<@ =item1.getname @>
...
<@ =item1.getcost @>
4: Display Array
We can also use some PHP code directly on our template page. In this detached MVC pattern, we should just write code here to manipulate these through actionobject and Viewresourcesconfig instances (possibly our custom bean can also) The data provided. In the above also content unit (' SALE/PAGECONTENT.SSP '), we use a PHP foreach Syntax (4) to iterate through the $products array. We can see $ in the Phpmvctabaction class above The products array is created and stored in Actionobject, similar to the $item1 bean above. We can use expressions to display product data in a Foreach loop:
<?php foreach ($products as $item) {?>
<tr>
&LT;TD class= ' Proditemdesc ' >
<@ =item.getname @>
</td>
&LT;TD class= ' Proditemvalue ' >
<@ =item.getcost @>
</td>
</tr>
<?php}?>
5: Show Viewresourcesconfig Properties
Finally, we show "area Manager" (5) on our content page from the Viewresourcesconfig attribute defined by the View-resources element:
<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>
But note in this example we used an object (Myviewresourcesconfig) that inherits the Viewresourcesconfig class to set some custom properties. We define an object that extends the Viewresourcesconfig class, The classname= "Myviewresourcesconfig" attribute is used in the configuration file, and the Myviewresourcesconfig class is defined in the file "myviewresourcesconfig.php". The Myviewresourcesconfig Class (classes/myviewresourcesconfig.php) implements the Setter/getter method to handle the custom attributes ("Areamanager"), This property we define in the View-resources node:
Class Myviewresourcesconfig extends Viewresourcesconfig {

-----Properties-----------------------------------------------------//

var $areaManager = ';

function Getareamanager () {
return $this->areamanager;
}

function Setareamanager ($areaManager) {
$this->areamanager = $areaManager;
}
We can now use an expression to implement "area Manager" on our page:
<@ =viewconfig.getareamanager @>
Note: In real-world applications, data can be obtained from relational databases.

Footer Unit

The footer unit is similar to the processing of the header unit discussed above. Footer Template file (' TPL/PAGEFOOTER.SSP ') like this:
<!--Page Footer-->
<span>
<@ =viewconfig.getcopyright @>
</span>
When the main page (including the included pages) is compiled, the expression in the footer is converted to the following:
<!--Page Footer-->
<span>
<?php print $viewConfig->getcopyright ();?>
</span>
The compiled header page is stored in the compiled template directory. The default compilation template directory is:
'./web-inf/tpl_c '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.