PHP-web application development: use templates (entry)

xiaoxiao2021-03-06  116

PHP-web application development: Template

Each developer designed for a larger PHP-web application has probably has the following experience: spend a lot of time writing hypertext statements, for page typography, combined with art, etc.; or integrated program code in and HTML static A lot of time spent on the 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.

Here is the template file to use: Using Template's Test </ Title> </ head> <body> <h2> This is a test file using a template! </ 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).</p> <p>Please think, if you do not need to set it, is this example you need 3 template files? Template file 1, page.ihtml <html> <head> <title> {pageTitle} </ title> </ head> <body bgcolor = "# ffffff"> <table border = 1 cellpadding = 4 cellspacing = 0 bgcolor = " #eeeeeee "> <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> <! -> </ TABLE> <! - end box.ihtml -> Template processing file, test.php <? php // introduced Template class include ("template.inc"); # Instantiate a template class, named $ t $ t = new template (); #777777053933 "page.ihtml", "box" => "box.ihtml")))); # 模 文件 文件 b 一, the reference name is ROWS $ T-> set_block, "row", " Rows "); # Settings Replace $ T-> set_var (array", "pageTe" => "hugo")); # 生 代 多 多 多, bignum for ($ I = 1; $ i) <= 3; $ i ) {$ n = $ i; $ nn = $ i * 10; # setting replacement $ t_var (array ("num" => $ n, "Bignum" => $ nn))) Analysis, the results of the analysis are added to the rear of Rows $ T-> Parse ("Rows", "Row", true);} # generate box, reproduce into Page $ T-> P Arse ("out", array ("box", "page")); # output the last result $ t-> p ("out");> Note:</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-126644.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="126644" 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.050</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 = 'Wte3nQkjbTt1lCXcvkWKVHYqF_2F13jLxkafWV27zUiD4D0Yi0QjAWyjaOWkQdhG9rjfZmC97UpBiWHZbe1lkIYw_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>