PHP. MVC template tag system (4)

Source: Internet
Author: User
Tags php foreach php print

Page Layout

In this unit, we will see how to use the template tag system to construct a standard template page. In this example, we use a simple HTML page layout. Please refer:

This page consists of multiple standard units, just as page designers and developers are familiar. the body of this page consists of three contained units: Header, content body, and footer. now let's take a look at these units and learn how to use the template tag system to implement them.

Page subject

The following code unit displays the subject:
The Page Body Layout
1
<@ SaleMonth = data. getValueBean ('sale _ month') @>
<@ SaleTitle = data. getValueBean ('sale _ title') @>
<@ DealHeading = data. getValueBean ('dest_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 = 'pagehead'>
<! -- Including the page header component -->
<! -- The base template base directory is "./tpl" -->
3 <@ include 'pageheader. ss' @>
</Td>
</Tr>

<! -- Page contents -->
<Tr valign = 'top'>
<Td class = 'pagecontent'>
<! -- Including the page contents component -->
4 <@ include 'sale/pageContent. ss' @>
</Td>
</Tr>

<! -- Page footer -->
<Tr>
<Td class = 'pagefooter '>
<! -- Including the page footer omponent -->
5 <@ include 'pagefooter. ss' @>
</Td>
</Tr>
</Table>

</Body>
</Html>

1: page declaration
The first interesting entry is the page declaration at the top of the page (1 ). we declare these variables on the page, so these variables can be used on the following page and include pages like the header.
2: page title
Next we will use the 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 command to insert the header template file to the page body. We will take a look at the header in the next subunit.
We only use the page to directly read the header, no matter where the page components are stored. this is a good opportunity to introduce the directory settings of the template tag system. by default, the template directory layout is as follows (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, as shown in the following figure:
<View-resources
...
TplDir = "./WEB-INF/tpl-admin"
TplDirC = "./WEB-INF/tpl_admin_C"
...
</View-resources>
4: page content subject
This is another include command used to insert the template file (4) to the subject. Note that the included file is located in the sales subdirectory of the template directory:
"./WEB-INF/tpl/sale/pageContent. ssp"
5: footer
It is also a containing command, just like the header.

Header Unit

In this example, the header template file ('pageheader. ss') is just a simple unit, like this:
<! -- Page Header -->
<Span>
<@ = ViewConfig. getAppTitle @>
</Span>
When the Subject Page (including the contained page) is compiled, the header expression is converted to the following:
<! -- Page Header -->
<Span>
<? Php print $ viewConfig-> getAppTitle ();?>
</Span>
The compiled page is stored in the compiling template directory. As mentioned above, the default compiling template directory is:
'./WEB-INF/tpl_C'

Page Content entity Unit

The page content subject template file is a bit complex. The content of the file ('sale/pageContent. ss') is shown as follows:
...
1
<@ Item1 = data-> getValueBean ("ITEM_1") @>
<@ Products = data-> getValueBean ("PRODUCTS_ARRAY") @>

2
<H4> <@ = dealHeading @> <@ = saleMonth @>

3
<B> Clearance deals </B>
<Table class = 'productstables '>
<Tr>
<Td class = 'proditemdesc'>
<@ = Item1.getName @>
</Td>
<Td class = 'proditemvalue'>
<@ = Item1.getCost @>
</Td>
</Tr>
</Table>

4
<B> Todays specials </B>
<Table class = 'productstables '>
<? 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 = 'productstables '>
<Tr>
<Td class = 'proditemdesc'>
<B> Area Manager: </B>
</Td>
<Td class = 'proditemdesc'>
<@ = ViewConfig. getAreaManager @>
</Td>
</Tr>
...
</Table>
1: more statements
The additional declaration shown on the top of the page (1) allows us to declare the page variables so that they can be used below. After the content is processed, these statements will be displayed as follows after compilation:
<? Php $ item1 = $ data-> getValueBean ("ITEM_1");?>
...
<? Php $ products = $ data-> getValueBean ("PRODUCTS_ARRAY");?>
2: use expressions to display the content unit title
Now we use two expressions (2) to display the title of the content unit. note that we declare these variables as "Global" variables at the top of the home page. after processing, the expression will convert 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 expressions to display some data entries
Now we can display some actual data (3 ). on this page, we access the product entry data in the ActionObject of the PhpMVCTabAction class. A simplified 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
// These cocould 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 :";
...

// 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 ('dest_heading ', $ dealHeading );
...

// Some float values we cocould 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 encoded 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 );
In the code above, we can see that the valueBeans entry. Bean data entry created and saved as ActionObject can be re-obtained on the template page:
<@ Item1 = data-> getValueBean ("ITEM_1") @>
We can display the entry value as follows:
<@ = Item1.getName @>
...
<@ = Item1.getCost @>
4: Display Arrays
We can also directly use some PHP code on our template page. in this separated MVC mode, we should write code here to manipulate the data provided by the ActionObject and ViewResourcesConfig instances (which may be available for our custom beans. the preceding content unit ('sale/pageContent. in SSPS '), we use a PHP foreach syntax (4) to read the $ products array cyclically. in the above PhpMVCTabAction class, we can see that the $ products array is created and saved in ActionObject, which is similar to the $ item1 Bean above. in the foreach loop, we can use expressions to display product data:
<? Php foreach ($ products as $ item) {?>
<Tr>
<Td class = 'proditemdesc'>
<@ = Item. getName @>
</Td>
<Td class = 'proditemvalue'>
<@ = Item. getCost @>
</Td>
</Tr>
<? Php }?>
5: Display ViewResourcesConfig attributes
Finally, we will display "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 = "areaManager" value = "Joe J. Blogs Esq."/>
</View-resources>
However, in this example, we use an object that inherits the ViewResourcesConfig class (MyViewResourcesConfig) to set some custom attributes. we define an object that extends the ViewResourcesConfig class. In the configuration file, use the className = "MyViewResourcesConfig" attribute, and the MyViewResourcesConfig class is defined in the file "MyViewResourcesConfig. php. myViewResourcesConfig class (classes/MyViewResourcesConfig. php) implements the setter/getter method to process custom attributes ("areaManager"). This attribute is defined in the view-resources node:
Class MyViewResourcesConfig extends ViewResourcesConfig {

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

Var $ areaManager = '';

Function getAreaManager (){
Return $ this-> areaManager;
}

Function setAreaManager ($ areaManager ){
$ This-> areaManager = $ areaManager;
}
Now we can use expressions to implement "Area Manager" on our page:
<@ = ViewConfig. getAreaManager @>
Note: Data in real applications can be obtained from relational databases.

Footer Unit

The footer unit is similar to the header Unit discussed above. The footer template file ('tpl/pageFooter. ss') is like this:
<! -- Page Footer -->
<Span>
<@ = ViewConfig. getCopyright @>
</Span>
When the Subject Page (including the contained page) 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 compiling template directory. The default compiling 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.