Template - why and how to use them in PHP

xiaoxiao2021-03-06  115

Template - why and how to use them in PHP

Whether you face a website created by a programmer (may be you) and a designer for creating at the same time? Don't you know how to make your work? Let me answer: Use fastTemplate to make your site easier to customize! Ok, you may want to know why you want to use fastTemplates. · You can change your entire site in a few seconds. Abstract programming, no rubbish html code · Designers don't need to care about all "fuzzy" code · Surprisingly · It is easier to reuse old templates (for ordinary Formation) The FastTemplate originated from a Perl package with the same name (can be found on the CPAN). You can download the PHP version from its home page (this site download address is: http://www.phpe.net/downloads/1.shtml). You only need one of the classes for classes (Class.fastTemplate.php). Let me first explain what is different between the use of the template to generate a page and simply passing the page output through Echo or Print. Simply use the Echo / Print method is suitable for writing short scripts, but you can't help you better organize and customize. The template gives you the ability to create multiple language sites on the other hand, just by changing a parameter. They can prompt you to care more about what you have to do. Don't be afraid before you start the encoding. It may take some time, but these spending will have returned to you with the development of the project. So how do I apply fastTemplate? First, you need to make a simple call: Passing a path, is a directory where you are stored in your template file. It returns an object that you can use it to assign a value, generate a page, and more. FastTemplate is based on such a hypothesis: a large page is made up of many small parts. Each section has a unique name. The smallest part is assigning a normal text string that has a unique name. This can be done by assign (name, "text");?>. Now, if your template contains {name}, FastTemplate knows your intention. In addition, FastTemplate needs to know how you want to call your template. You need to give it a prompt by passing the associated array (Associative Array);?> To give it a prompt. Define (array (foo => "foo.tpl", bar => "bar.tpl"));?>> These assignments will give foo and bar to different files (name foo). TPL and Bar.tpl). Now you want FastTemplate to replace all {macros} in template foo to the corresponding value. Implemented by issuing a command PARSE (PageContent, "Foo"); This command will assign the contents of the template "foo" to PageContent. Of course, we haven't finished yet, because the template bar is the main page definition, fastTemplate needs to replace the {pageContent} macro.

We also need to assign a value to PageTitle, as follows: Assign (PageTitle, "FooBar Test"); $ TPL-> PARSE (Main, "Bar");?> Easy, isn't it? We only need to output it: FastPrint (main);?> The following three files show more details in the actual exercise. I don't know how to live in real life - your designer will be happy, your boss will smile because you can do more in a shorter time. Bar.tpl feature world - {pageTure} </ title> </ head> <body bgcolor = black text = white> <h1> {pageTiTle } </ H1> {pageContent} </ body> </ html> foo.tpl <! - foo.tpl -> It doesn't show anything. Please see {name}. Demo.php3 <? Php include "class.fasttemplate.php3"; $ TPL = new fasttemplate ("."); $ TPL-> Define (array (foo => "foo.tpl", bar => "bar.tpl")); $ TPL-> Assign (Name, "ME"); $ TPL-> Assign (PageTitle, "Welcome!"); $ TPL-> PARS (PageContent, "foo"); $ TPL-> PARSE (Main, "Bar"); $ TPL-> FastPrint (Main);> Create a whole form I also wrote a short example to demonstrate how to generate the entire form through a single line template. It is very effective because you still don't need to modify the HTML document directly. We add a template to create an HTML form behind a defined unique named name. This can be implemented by adding a "." To the template name when calling $ TPL-> PARSE ().</p> <p><? php // Assign the contents of the template foo to TPL1 $ TPL-> PARSE (TPL1, "Foo"); // Attach the template bar after TPL1 $ TPL-> PARSE (TPL1, ".bar") ;?> Page.tpl <html> <head> <title> feature world - {page_title} </ title> </ head> <body bgcolor = black text = white> <h1> {page_title} </ h1> {page_content } </ Body> </ html> Table.tpl <Table> <TH> <TH> Name </ TH> <TH> SIZE </ TH> </ TR> {TABLE_ROWS} </ TABLE> TABLE_ROW.TPL <TR > <Td> {filename} </ td> <td> {filesize} </ td> </ tr> yad.php3 <? Php include "class.fastTemplate.php3"; function initializetemplates () {global $ TPL; $ TPL = new fasttemplate ("."); $ TPL-> Define (array (page => "page.tpl", table_row => "Table_row.tpl");} Function ReadcurrentDirectory () {Global $ TPL; $ HANDLE = OpenDir ("."); While ($, $, $ tpl-> assign (filename, $ filename); $ TPL-> Assign (FileSize, FileSize ($ filename)); $ TPL-> PARSE (Table_Rows, ".Table_Row");} closedir ($ handle); $ TPL-> PARSE (Page_Content, "Table");} Function PrintPage ($ TITLE) {Global $ TPL; $ TPL-> Assign (Page_Title, $ TITLE); $ TPL-> PA RSE (Final, "Page"); $ TPL-> FastPrint (FINAL);} InitializeTemplates (); ReadCurrentDirectory (); PrintPage ("YET ANOTHER DEMO");?> Speed ​​Discussion "OK," You may say, " Everything is too good. But does it affect the speed of my website? "No, your website may become faster. A simple reason is: because you as a programmer care is to design your app and write code, your code will be more efficient, handle the same task easier And faster. So, you may consider add another reason to why consider using fastTemplate in your project. If you just want to convert an existing Web site, performance can not be Will be noted.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-126643.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="126643" 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.049</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 = 'wbTD_2BPI9S1k7wO23SkF2XUiAtqG833iDR9zUfFHnHrs5QyndSPjCvAZ9MDB8bpgN8vfdf1IxYSFlytPb'; 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>