The main task of the VIEW layer of the MVC mode is to make the page and the result of the display work. During the implementation of the PHP, it is mainly reflected as a template (using template, can reach the PHP code and HTML code separation purposes, so code and page The maintenance is more convenient, easy to manage and replace the page, the analysis process of the programmer, the division of labor): First, the Controler layer gets the data from the Model layer, and the Controler layer will hand over the data to the View layer, The interface of the View layer will pass the data to the template parsing class in a certain manner, and finally, the template resolution class parsing the data into the template and then display.
Here is a specific implementation example
Directory Structure
| - ClassRendertest.php // Test Analysis Classlist.html | - StudentRtest.php // Test Resolution StudentList.html | - Render / TemplateParser.php // Template Resolution Class | - Render / Render.php // All classes of the template Basic class | - render / studentrender.php // Analytical Template StudentList.html Class | - Render / ClassRender.php // Analytical Template ClassList.html Class | - Template / StudentList.html // Template File | - Template / Classlist.html // template file
Note: 1. Here the template resolution class selects simple "templateParser.php". According to individual needs, you can use any template parsing class; 2. If each template resolution is directly called "TemplateParser.php", may There is a lot of repetition code, which is not allowed by OO thinking. Therefore, "render.php" is packaged, and then the render class in "render.php" is used to extension, parsing different file templates; 3. Different templates parsing class, the method of use is different Their packaging is also different. 4, "Studenter.php" "classrender.php" is the need to meet the "StudentList.html" "ClassList.html", which is used to satisfy "StudentList.html".
Class Num: _Class_ TH> TR> end_classinfo _ table> Current Class's Student:
ID TH>
Name th>
Score th> tr> begin_studentlist_
_sid _ td>
_sname _ td>
_score _ td> TR> end_studentlist_ table> File 3: TemplateParser.php The following template resolution class is a simple template parsing class, which is temporarily written, and there is very few functions. But here can meet the needs of this article. At the same time, if there is no contact with template parsing, the implementation method of template parsing has certain questions. You can study this simple class to achieve parsing. The code should be able to understand.
This parsed class has its own template structure, "block" (where to loop displays) is as follows:
Begin_ Your block name _ ... HTML code ....... _ _ ... HTML code ....... End_ you Block name_
The definition of variables is as follows:
...... HTML code ....... _ _ _ ... HTML code .......
Specific "block" and "variable" use references to 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 Value * / Function SetBlockData ($ BLOCKNAME, & $ DATA, $ REC = false) {$ this-> data [$ blockna Me] = & $ 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 parse * * @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
? Parser-> setData (' now ', DATE (' YMD 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 ('YMD H: i: s'); $ this-> paser-> setblockdata ('Classlist', & $ DATA ['Class'], True;}}?>
-------------------------------------------------- Below two are test files, the first is simpler ------------------------------------- ------------- Test file 1: ClassRendertest.php
php / * * incrude "class list show render" * / request_once "render" render / classrender.php "; / * * data for parse * * include (class list) * / $ data = array ('class" => array ('1' => array ('CID' => 1, 'CNAME' => 'Class One', 'grade' => 3, 'Class' => 1), '2' => Array ('CID' => 2, 'CNAME' => 'Class Two', 'grade' => 3, 'Class' => 2),' 3 '=> Array (' CID '=> 3,' CNAME '=>' Class Three ',' grade '=> 4,' Class '=> 1),' 4 '=> Array (' CID '=> 4,' CNAME '=>' Class Four ',' grade '=> 4,' Class' => 2), '5' => Array ('CID' => 5, 'CNAME' => 'Class Five', 'GRADE' => 5, 'Class' => 1))); / * * DO Template Parse * / Dorender ($ data); / * * May See As Controler's action, use to piers {$ render = new classrender (); $ render-> initdata ($ render-> initdata Data); $ render-> show ();}?> Run results:
current time is: 2004-05-10 23:51:26
current school class list:
ID TH>
NAME TH>
grade th>
Class th> tr>
1 td>
Class One TD>
3 TD>
1 TD> TD>
Class Two TD>
3 TD>
2 TD> TR>
3 TD>
Class Three TD>
4 TD>
1
TD>
4 TD >
Class Four TD>
4 TD>
2
TD>
Class Five TD>
5 TD>
1 TD> TD> Table> Test File 2: StudentRTest.php
php / * * include "Shudent List show render" * / request_once "render" render "render / studentrender.php"; / * * Data for parse * * include (one class info) * / $ data = Array ('Class' => Array (' CID '=> 1,' CNAME '=>' Class One ',' Grade '=> 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 controller's action, use to parse template * / function Dorender (& $ data) {$ render = New studentrender (); $ render-> initdata ($ data); $ render-> show ();}?> Run results: