PHP-web application development: Template

xiaoxiao2021-03-06  71

PHP-Web application development: Use template author: Wang Kai each conducted large-developer of PHP-Web application design probably have the following experience: spend a lot of time writing hypertext statement for page layout, doubles Mercure, or the like; spend a lot of time in the integrated program code in and the HTML static page. Indeed, using a scripting language development web application is not easy to separate the data processing and data display, but in the case of multiplayer cooperation, if data and display are unable to separate, it will greatly affect the efficiency of development, professional division of labor. In order to solve this problem, PHP also provides its own solution, there are many, this article mainly introduces the Template class in phplib. 1 Template Treatment The design template processing class is mainly necessary to complete the following tasks: • Read the displayed HTML code from the template file. · Combine the template file and the actual data generated to generate the result of the output. • Allows multiple templates simultaneously. · Allow the nested template. • Allows a separate portion of the template to be processed. Increte the above tasks, the design objectives of the template class are: from the displayed HTML code from multiple template files, replace these display code to replace the data derived from the PHP program calculation, then output according to certain order . Among them, the replacement portion can be freely set. Reading the displayed HTML code uses a read file mode template file and data combination using a regular replacement to process multiple templates with array storage. The main idea of ​​the nested implementation of the template is: Template and output (any intermediate analysis result) can be replaced by colleagues, and can be implemented. The processing of individual sections is configured by setting labels in a template file and then bind to the regular replacement to control, and implement some replacement. 2 Template Treatment The implementation of the Template.inc in phplib, a total of 345 lines of code, with a detailed note. The following list is listed in the main function, and it is reviewed for reading: 1) Function set_file ($ handle, $ filename = ") Line 77, read file 2) Function set_var ($ VARNAME, $ VALUE =") LINE 119, setting mapping Data - Replacement Variable 3) Function Set_block ($ PARENT, $ Handle, $ Name = ") Line 96, Settings Target 4) Function Subst ($ Handle) Line 136, Perform Data Replacement 5) Function Parse ($ TARGET, $ HANDLE , $ append = false) Line 165, the combination of template files and data 6) Function P ($ VarName) line 268, output processing result Note: I downloaded the pHP-lib7.2c of the Template.inc file in the template.inc file Less one "/", add normal.

3 Template Treatment The use of 3.1 The most basic example In order to see, it is assumed here that the template file, the file using the template PHP file and the template processing class is placed in the same directory. The habits in phplib are the suffix of the template file using the IHTML suffix. Below is the template file to use:

Test </ Title> </ head> <body> <body> <body> <body> <body> <body> <body> <h2> This is a test file! </ h2> Current time is {currentTime}! </ body> </ html> Note: The template file and the usual HTML file are similar, the only difference is that "{}" is enclosed by the template processing class. Dynamic content variables. Next, use the template processing class to handle the above template: <? // introduced Template class include ("template.inc"); // Get data required to replace $ TIMENOW = Date ("YMD H: i: s", Time ()); // instantiate a Template class $ template = new template (); // Load Test.ihtml Template $ TEMPLATE-> Set_File ("Handle1", "Test.ihtml"); // Using $ TIMENOW CurrentTime $ Template-> set_var ("CurrentTime", $ TIMENOW); // Template to Template-> Parse ("Output", "Handle1"); // Output last Result $ TEMPLATE- > P ("OUTPUT");?> Note: If you just want to use the template class in phplib, you only need to contain the template.inc class on the file header. When you create a Template object, you can specify a template file path, such as: New Template ("/ HTDOCS / Apps / Templates /"), the default is the current path. 3.2 Template Nested and Block Settings The following example comes from the reference manual with phpo, strong comprehensive, point herein is that the purpose of the setting block is independent of nested, but this example contains both. Please read it carefully, block settings to avoid this: I originally completed in a template file (static page), and partial loop content will be made separately into a template file in a template file (static page). Please think, if you do not need to set it, is this example you need 3 template files? Template file 1, Page.iHTML</p> <p><html> <head> <title> {pageTitle} </ title> </ head> <body bgcolor = "# ffffff"> <table border = 1 cellpadding = 4 cellspacing = 0 bgcolor = "# eeeeeeeee"> <TR> <TD COLSPAN = 2> <h1> {pageTitle} </ h1> <td> </ tr> <tr> <td> {out} </ td> <TD> Content </ TD> </ TR> < / Table> </ body> </ html> Template file 2, Box.ihtml <! - start box.ihtml -> <table border = 1 bgcolor = "# ccccc" cellpadding = 4 cellspacing = 0> <TR> <TD colspan = 2> <b> {title} </ b> </ td> </ tr> <! - begin row -> <tr> <td> {num} </ td> <td> { Bignum} </ tr> <! - End Box.iHtml -> Template Processing File, Test.php <? Php // Introduced Template Class Include ("Template. Inc "); # instantiate a Template class, named $ t $ t = new template (); #7770149393939393 Box "=>" box.ihtml "))); # 模 文件 文件 b r, the reference name is Rows $ T-> set_block (" Box "," Row "); # set the replacement $ T-> set_var ("Title" => "TestPage", "PageTitle" => "hugo")); # 生 代 多 多 多, bignum for ($ I = 1; $ I <= 3; $ i ) {$ n = $ i; $ nn = $ i * 10; # setting replace $ t-> set_var (array ("num" => $ n, "Bignum" => $ NN)); # analysis, analysis Result Add to ROWS $ T-> Parse ("Rows", "Row", True);} # Generate Box, regenerate into Page $ T-> Parse ("OUT", Array ("Box", "Page"); # Output Last result $ t-> p ("out");> Note: Here: Naming of variables in the template file in the Page.iHTML template file is used "OUT ". The cyclic values ​​are used to use the database class to combine the generation of the data and the database application. The execution result is as follows:</p> <p>Template Nested and Block Setup Execution Execution Figure 4 Small junction This article mainly introduces the design, implementation, and method of use of Template classes in phpLIB. Of course, there are many other PHP template schemes, such as fastTemplates that are evolved from Perl. At present, the Internet team uses another solution. The main implementation is the template file. The use of EVAL to achieve the combination of data and template files, and the management of the template file is relatively simple, but lacks The mode of file storage. Some attempts currently doing are combining both and want to improve on the basis of phpLib's Template class. The initial target task has two: 1. Extension to the database support when reading a template file, which enhances the flexibility and the use of database management tools 2, and the combination of simplified template files and data can be used when necessary. In most cases, there is no need to set the variables in the template file in the handler (data variable map) again. I also hope that the reader, PHP program enthusiasts participate together, pay more attention, I wish good luck!</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-91573.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="91573" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.036</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'etcHCVR_2BwFPTlFnKPCNNCUtHQ3picWvnLfi2kFKKs_2BHNXTcfXeFTQxiGUZu9jZx0jremGMgKMREmJJMzELQiJg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>