Template framework for making static sites with PHP

xiaoxiao2021-03-06  111

Template framework for making static sites with PHP

The template can improve the structure of the website. This article describes how to use a template control page layout in a website composed of a large number of static HTML pages through a new function and template class. Outline: ========================================================================================================================================================================================================】 =================================== Separation function and layout first let's take a look at the two main two main application templates purpose:

Separation Function (PHP) and Layout (HTML) Avoiding Page Elements The first purpose is to talk about the most purpose, it envisages the situation: a group of programmers write PHP scripts used to generate page content, and another set of designs The staff design HTML and graphics to control the final appearance of the page. The basic idea of ​​separation function and layout is to make the two groups of people can write and use independent set of files: programmers only need to care about files that contain only PHP code, do not need to care about the appearance of the page; and page designers can use themselves The most familiar visual editor design page layout, there is no need to worry about destroying any PHP code embedded to the page. If you have seen a few tutorials about the PHP template, then you should have already understood the working mechanism of the template. Consider a simple page part: Above the page is the header, the left is the navigation bar, the rest is the content area. This website can have the following template file: template example </ title> </ head> <body> <table> <tr> <td> {Header} </ td> </ tr> <tr> <td> {leftnav} </ td> <td> {content} </ td> </ tr> </ table> </ body> </ html> <-! header.htm -> <img src = "sitelogo.jpg"> <-! leftnav.htm -> <br> <a href="foo"> Foo </a> <br> < A href = "bar"> bar </a> can be seen from these templates: the main template controls the layout of the entire page; the Header template and the Leftnav template control the public elements of the page. The marker "{}" inside is the content placeholder. The main benefit of using templates is that interface designers can edit these files in their own will, such as setting font, modifying colors, and graphics, or completely changing the layout of the page. Interface designers can edit these pages with any common HTML editor or visualization tool, because these files contain only HTML code, without any PHP code. All PHP code is saved to a separate file, which is the file actually called by the page URL. The web server parsing the file via the PHP engine and then returns the result to the browser. In general, PHP code always dynamically generates page content, such as querying the database or performing some calculation.</p> <p>Here is an example: <? Php // example.php require ('class.fastTemplate.php'); $ TPL = new fasttemplate ('.'); $ TPL-> Define (array ('main' => "main .htm ',' header '=>' Header.htm ',' Leftnav '=>' Leftnav.htm ')); // The PHP code setting $ Content makes it contains the right page content $ TPL-> Assign ('Content', $ Content); $ TPL-> Parse ('Header', 'Header'); $ TPL-> Parse ('Leftnav', 'Leftnav'); $ TPL-> PARSE ('main', ' Main '); $ TPL-> FASTPRINT (' main ');> Here we use the popular FastTemplate template class, but its basic ideas are the same for many other templates. First of all, you instantiate a class, tell it where to find the template file and which template file corresponds to the page; then generate the page content, give the result to the identifier of the content; then parse each template file, The template class will perform the necessary replacement operation; finally output the resolution result to the browser. This file is completely constructed by PHP code, does not contain any HTML code, which is its biggest advantage. Now, PHP programmers can focus on writing code that generates page content without having to format the final page for how to generate HTML to correctly format the final page. You can use this method and the above file to construct a complete website. If the PHP code is generated by the query string in the URL, such as http://www.foo.com/example.php?article=099, you can construct a complete magazine website. It is easy to see that there is a second benefit using template. As shown in the above example, the navigation strips on the left side of the page are saved as a file, and we can change the navigation bar left on the left side of the website only by editing this template file. Avoiding page elements repeating "This is really good", you might think, "My website is mainly composed of a large number of static pages. Now I can remove their public part from all pages, and I want to update these public parts. Too much trouble. After I can make a unified page layout that is easy to maintain with template. "But things are not as simple as it," a large number of static pages "said the problem. Consider the above example. This example actually has only an Example.php page, which can generate all the pages of the entire website because it uses the query string in the URL to dynamically construct the page from the information source such as the database. The websites in most people in us do not necessarily have database support. Most of our website consists of static pages, then use PHP here, there are some dynamic functions, such as search engines, feedback forms, etc. So how do I apply templates on this website? The easiest way is to copy a PHP file for each page, and then set the variables representing the content in each page to the appropriate page content. For example, suppose there are three pages, they are homepage (home), About and Products, we can generate them with three files.</p> <p>The content of these three files is like: <? Php // Home.php Require ('Class.FastTemplate.php'); $ TPL = New FastTemplate ('.'); $ TPL-> Define (Array ('main '=>' main.htm ',' header '=>' Header.htm ',' Leftnav '=>' Leftnav.htm ')); $ Content = "<p> Welcome to </ p> <img src = /"demo.jpg / "> <p> I hope you can like this website </ p>"; $ TPL-> Assign ('Content', $ Content); $ TPL-> PARSE ('Header', 'Header' ); $ TPL-> PARSE ('Leftnav', 'Leftnav'); $ TPL-> PARSE ('main', 'main'); $ TPL-> Fastprint ('main');?> Obviously, this method There are three questions: We must copy these complex to each page, involving the PHP code of the template, which makes the page difficult to maintain like repeating public page elements; now the file has mixed HTML and PHP code; assigning the content variable It is very difficult because we have to handle a lot of special characters. The key to solving this problem is to separate the PHP code and HTML content, although we cannot delete all HTML content from the file, but can remove most PHP code. Template framework for static sites First, we write template files as all of the page public elements and page overall layouts, and then remove the public part from all pages, leaving only page content; next to each page Three lines of PHP code as follows: <? Php <! - Home.php -> <? Php request ('prepend.php');?> <? Php pagestart ('Home');?> <H1 > Hello </ h1> <p> Welcome to access </ p> <img src = "demo.jpg"> <p> I hope you can like this website </ p> <? Php pagefinish ();?>?> This method basically solves the various problems mentioned earlier. There are only three lines of PHP code in the file, and there is no row of code directly to the template, so it is necessary to change the possibility of these code. In addition, since the HTML content is outside the PHP tag, there is no processing problem of special characters. We can easily add this three-line PHP code to all static HTML pages. The Require function introduced a PHP file that contains all the required PHP code related to the template. The Pagestart function sets the template object and the page title, the PageFinish function parsing the template and generates the result to send it to the browser. How is this implementation? Why is HTML in the file before calling the PageFinish function to the browser? The answer is a new feature of PHP 4, which allows the contents of the output to the browser to the buffer.</p> <p>Let's take a look at the specific code of prepend.php: <? PHP Require ('Class.FastTemplate.php'); Function Pagestart ($ TITLE = ') {Global $ TPL; $ TPL = New FastTemplate ('. ') $ TPL-> Define (array ('main' => 'main.htm', 'header' => 'Header.htm', 'Leftnav' => 'Leftnav.htm')); $ TPL-> Assign 'Title', $ TITLE; OB_START ();} Function PageFinish () {Global $ TPL; $ Content = Ob_Get_Contents (); OB_END_CLEAN (); $ TPL-> Assign ('Content', $ Content); $ TPL- > Parse ('Header', 'Header'); $ TPL-> Parse ('Leftnav', 'Leftnav'); $ TPL-> Parse ('main', 'main'); $ TPL-> Fastprint ('main ');}?> The Pagestart function first creates and sets a template instance, and then enable the output cache. Thereafter, all HTML content from the page itself will enter the cache. The PageFinish function takes out the contents in the cache, then specify these content in the template object, finally parsing the template and outputs the finished page. This is the entire work process of the entire template frame. First, write a template that contains all the public elements of the website, then removes all public page layout code from all pages, in which the PHP code will never need to be changed; then add the fastTemplate class file and prepend.php to the included path So you get a website that can focus on a page layout, it has better reliability and maintainability, and the large-scale modification of the site level has become quite easy. This article downloads the package contains a running example website, which is more detailed than the previous code annotation. The FastTemplate class can be found at http://www.thewebmasters.net/, the latest version number is 1.1.0, there is a small patch to ensure that the class is properly run in PHP 4. The classes in this article download code have been corrected by this patch.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-126634.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="126634" 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.046</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 = 'dne3SR3uy7RGIUJGI9_2BEml95YHjnnpmeVBL4d2IthCEhgh9GOulS5NdA1fAM9SLhCswsUJXtKguSrAI2I_2FnX_2BQ_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>