Start your MVC (4) to implement the View layer with PHP

zhaozj2021-02-12  178

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".

Document 1: ClassList.html

current time is: _now_

current school class list:

Begin_ClassList_
ID NAME GRADE < Th> Class
_CID _ _CNAME _ _GRADE _ _Class _ END_CLASSLIST _ File 2: StudentList.html

current time is: _now_

current class is:

BEGIN_classinfo_
class id: _cid _ class name: _cname _ Class grade: _Grade _ Class Num: _Class_ end_classinfo _
Current Class's Student:
ID Name Score begin_studentlist_
_sid _ _sname _ _score _ end_studentlist_ 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

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 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 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

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 NAME grade Class
1 Class One 3 1 Class Two 3 2
3 Class Three 4 1 4 Class Four 4 2 Class Five 5 1 Test File 2: StudentRTest.php

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:

转载请注明原文地址:https://www.9cbs.com/read-6126.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.035, SQL: 9