Use PHP to start your MVC (4) Implementation of the view layer

Source: Internet
Author: User

The main task of the view layer in MVC mode is to display pages and results. In PHP implementation, it is mainly reflected as a template (using a template, PHP code and HTML code separation can be achieved, so that code and page maintenance is much more convenient, easy to manage and page replacement, can truly divide the division of labor of programmers and artists) parsing process:
First, the controler layer obtains data from the model layer.
Second, the controler layer delivers the data to the view layer.
Thirdly, the view layer interface transmits data to the template parsing class in a certain way,
Finally, the template parsing class parses the data to the template and then displays it.

The following is an example.
Directory structure

|-Classrendertest. php // test classlist.html
|-Studentrendertest. php // test studentlist.html
|-Render/templateparser. php // template parsing class
|-Render/render. php // parse the basic classes of all classes in the template
|-Render/studentrender. php // the class of the intent template studentlist.html
|-Render/classrender. php // the class of the callback template classlist.html
|-Template/studentlist.html // Template File
|-Template/classlist.html // Template File

Note:
1. The template parsing class uses the simple "templateparser. php". You can select any template parsing Class Based on your needs;
2. If "templateparser. php" is directly called for parsing every template, a large number of repeated code may appear, which is not allowed by oo ideas. Therefore, "Render. php" is used to pack it, and then the render class in "Render. php" is extended to parse different file templates;
3. Different template parsing classes use different methods and their packaging methods may be different.
Required.

File 1: classlist.html

Current time is: _ now _ <br>
Current school class list:
<Table border = 1>
<Tr>
<TH> id </Th>
<TH> name </Th>
<TH> Grade </Th>
<TH> class </Th>
</Tr>
Begin_classlist _
<Tr>
<TD> _ CID _ </TD>
<TD> _ cname _ </TD>
<TD> _ grade _ </TD>
<TD> _ class _ </TD>
</Tr>
End_classlist _
</Table>

File 2: studentlist.html

Current time is: _ now _ <br>
Current class is:
<Table border = 1>
Begin_classinfo _
<Tr>
<TH> class ID: _ CID _ </Th>
<TH> Class Name: _ cname _ </Th>
<TH> class grade: _ grade _ </Th>
<TH> class num: _ class _ </Th>
</Tr>
End_classinfo _
</Table> <br>
Current class's student:
<Table border = 1>
<Tr>
<TH> id </Th>
<TH> name </Th>
<TH> score </Th>
</Tr>
Begin_studentlist _
<Tr>
<TD> _ SID _ </TD>
<TD> _ sname _ </TD>
<TD> _ score _ </TD>
</Tr>
End_studentlist _
</Table>

File 3: templateparser. php
The following template parsing class is a simple template parsing class temporarily written by the author. It has few functions and has no real value for use. However, it can satisfy the needs described in this article.
At the same time, if you have never touched on Template parsing before and have doubts about the implementation method of template parsing, you can study the method of implementing parsing in this simple class, the code is quite simple and should be understandable.

This parsing class has its own template structure. The definition of "Block" (where the loop is to be displayed) is as follows:

Begin _ your block name _
......Html code .......
_ Name of the variable in your block _
......Html code .......
End _ your block name _

The variables are defined as follows:

......Html code .......
_ Name of the variable in your block _
......Html code .......

For more information about how to use block and variable, see the preceding two templates.

<? PHP
/*
* Template parse class
*/
Class templateparser {
/*
* @ Var template root directory
*/
VaR $ root = '';
/*
* @ Var template filename
*/
VaR $ TPL = '';
/*
* @ Var data for parse Template
*/
VaR $ DATA = array ();
/*
* @ Var template parse result
*/
VaR $ result = '';
/*
* Construct Function
*/
Function templateparser ($ root = ''){
$ This-> root = $ root;
}
/*
* Set template file name
*/
Function loadtemplatefile ($ tplfile ){
$ This-> TPL = $ tplfile;
}
/*
* Set global VaR value;
*
* @ Param $ varname global var name
* @ Param $ data var's Value
*/
Function setdata ($ varname, $ data ){
$ This-> data ['_ all _'] [$ varname] = $ data;
}
/*
* Set global VaR value;
*
* @ Param $ blockname template block name
* @ Param $ data VaR value
* @ Param $ rec VaR value
*/
Function setblockdata ($ blockname, & $ data, $ rec = false ){
$ This-> data [$ blockname] = & $ data;
$ This-> rec [$ blockname] = $ REC;
}
/*
* Parse template action
*/
Function parse (){
$ Tplstr = file_get_contents ("{$ this-> root}/{$ this-> TPL }");
Foreach ($ this-> data as $ block => $ value ){
$ Tag = "| begin _ {$ block} _ (. *) End _ {$ block} _ | SM ";
Preg_match ($ tag, $ tplstr, $ tmpdata );
If ($ tmpdata [1]! = NULL ){
$ Tmpstr = '';
If ($ this-> rec [$ block]) {
Foreach ($ value as $ subvalue)
$ Tmpstr. = $ this-> _ parseblock ($ tmpdata [1], $ subvalue );
} Else {
$ Tmpstr. = $ this-> _ parseblock ($ tmpdata [1], $ value );
}
}
$ Tplstr = preg_replace ("| begin _ {$ block} _ (. *) End _ {$ block} _ | SM", $ tmpstr, $ tplstr );
}
$ Tplstr = $ this-> _ parseblock ($ tplstr, $ this-> data ['_ all _']);
$ This-> result = $ tplstr;
}
/*
* Parse Block
*
* @ Param $ STR string one block string
* @ Param $ data array data for parse
*/
Function _ parseblock ($ STR, $ data ){
Foreach ($ data as $ key => $ value ){
$ Keys [] = "_ {$ key }_";
$ Values [] = "$ value ";
}
Return str_replace ($ keys, $ values, $ Str );
}
/*
* Return parse result
*/
Function get (){
Return $ this-> result;
}
/*
* Show parse result
*/
Function show (){
Echo $ this-> result;
}
}
?>

File 4: render. php

<? PHP
/*
* Include template parser class
*/
Require_once "templateparser. php ";
/*
* Base render class
*/
Class render {
/*
* @ Var object of template parser class
*/
VaR $ parser = "";
/*
* Instruct Function
*/
Function render ($ root, $ tplfile ){
$ This-> parser = new templateparser ($ root );
$ This-> parser-> loadtemplatefile ($ tplfile );
}
/*
* Add data to template parser
*
* @ Param $ data array () data for parse
*/
Function initdata (& $ data ){
Return;
}
/*
* Show template parse result
*/
Function show (){
$ This-> parser-> parse ();
$ This-> parser-> show ();
}
}
?>

File 5: studentrender. php

<? PHP
/*
* Include base class
*/
Require_once "Render. php ";
/*
* Shudent list show render
*/
Class studentrender extends render {

Function studentrender (){
Parent: render ('./template', 'studentlist.html ');
}

Function initdata ($ data ){
$ This-> parser-> setdata ('now ', date ('Y-m-d h: I: s '));
$ This-> parser-> setblockdata ('classinfo', $ data ['class'], false );
$ This-> parser-> setblockdata ('studentlist', $ data ['student '], true );
}
}
?>

File 6: classrender. php

<? PHP
/*
* Include base class
*/
Require_once "Render. php ";
/*
* Class list show render
*/
Class classrender extends render {

Function classrender (){
Parent: render ('./template', 'classlist.html ');
}

Function initdata (& $ data ){
$ This-> parser-> setdata ('now ', date ('Y-m-d h: I: s '));
$ This-> parser-> setblockdata ('classlist', & $ data ['class'], true );
}
}
?>

 

--------------------------------------------------
The following two are test files. The first one is simpler.
--------------------------------------------------
Test File 1: classrendertest. php

<? PHP
/*
* Include "class list show render"
*/
Require_once "render/classrender. php ";
/*
* Data for parse
*
* Include (class list)
*/
$ DATA = array ('class' => array ('1' => array ('cid' => 1, 'cname' => 'class one ', 'case' => 3, 'class' => 1 ),
'2' => array ('cid' => 2, 'cname' => 'class two', 'grad' => 3, 'class' => 2 ),
'3' => array ('cid' => 3, 'cname' => 'class three ', 'grad' => 4, 'class' => 1 ),
'4' => array ('cid' => 4, 'cname' => 'class four ', 'grade' => 4, 'class' => 2 ),
'5' => array ('cid' => 5, 'cname' => 'class five', 'grad' => 5, 'class' => 1 )));
/*
* Do template parse
*/
Dorender ($ data );
/*
* May see as controler's action, use to parse Template
*/
Function dorender (& $ data ){
$ Render = new classrender ();
$ Render-> initdata ($ data );
$ Render-> show ();
}
?>

Running result:

Current time is: 23:51:26 <br>
Current school class list:
<Table border = 1>
<Tr>
<TH> id </Th>
<TH> name </Th>
<TH> Grade </Th>
<TH> class </Th>
</Tr>
<Tr>
<TD> 1 </TD>
<TD> Class One </TD>
<TD> 3 </TD>
<TD> 1 </TD>
</Tr>
<Tr>
<TD> 2 </TD>
<TD> Class Two </TD>
<TD> 3 </TD>
<TD> 2 </TD>
</Tr>
<Tr>
<TD> 3 </TD>
<TD> Class Three </TD>
<TD> 4 </TD>
<TD> 1 </TD>
</Tr>
<Tr>
<TD> 4 </TD>
<TD> Class Four </TD>
<TD> 4 </TD>
<TD> 2 </TD>
</Tr>
<Tr>
<TD> 5 </TD>
<TD> Class Five </TD>
<TD> 5 </TD>
<TD> 1 </TD>
</Tr>
</Table>

Test file 2: studentrendertest. php

<? PHP
/*
* Include "shudent list show render"
*/
Require_once "render/studentrender. php ";
/*
* Data for parse
*
* Include (one class info) and (one class's student list)
*/
$ DATA = array ('class' => array ('cid' => 1, 'cname' => 'class one', 'grad' => 3, 'class' => 1 ),
'Student '=> array ('1' => array ('sid' => 1, 'sname' => 'stu one', 'score '=> 100 ),
'2' => array ('sid '=> 2, 'sname' => 'stu two', 'score' => 90 ),
'3' => array ('sid '=> 3, 'sname' => 'stu three', 'score '=> 80 ),
'4' => array ('sid '=> 4, 'sname' => 'stu four', 'score '=> 95 ),
'5' => array ('sid '=> 5, 'sname' => 'stu five', 'score' => 55 )));
/*
* Do template parse
*/
Dorender ($ data );
/*
* May see as controler's action, use to parse Template
*/
Function dorender (& $ data ){
$ Render = new studentrender ();
$ Render-> initdata ($ data );
$ Render-> show ();
}
?>

Running result:

Current time is: 23:52:22 <br>
Current class is:
<Table border = 1>
<Tr>
<TH> class ID: 1 </Th>
<TH> Class Name: Class One </Th>
<TH> class grade: 3 </Th>
<TH> class num: 1 </Th>
</Tr>
</Table> <br>
Current class's student:
<Table border = 1>
<Tr>
<TH> id </Th>
<TH> name </Th>
<TH> score </Th>
</Tr>
<Tr>
<TD> 1 </TD>
<TD> Stu one </TD>
<TD> 100 </TD>
</Tr>
<Tr>
<TD> 2 </TD>
<TD> Stu two </TD>
<TD> 90 </TD>
</Tr>
<Tr>
<TD> 3 </TD>
<TD> Stu three </TD>
<TD> 80 </TD>
</Tr>
<Tr>
<TD> 4 </TD>
<TD> Stu four </TD>
<TD> 95 </TD>
</Tr>
<Tr>
<TD> 5 </TD>
<TD> Stu five </TD>
<TD> 55 </TD>
</Tr>
</Table>

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.