Templates can improve the structure of your website. This article explains how to use a new feature of PHP 4 and the template class to skillfully use templates to control page layout in a website composed of a large number of static HTML pages.
Outline:
======================================
Separate functions and layout
Avoid duplication of page elements
Template framework for static websites
======== ===========================
Separating functionality and layout
First let’s take a look at the application template Two main purposes:
Separate functionality (PHP) and layout (HTML)
Avoid duplication of page elements
The first purpose is the most talked about purpose, it Imagine a situation where one group of programmers writes the PHP scripts that generate the page's content, while another group of designers designs the HTML and graphics to control the page's final appearance. The basic idea of separating functionality and layout is to enable these two groups of people to write and use an independent set of files: programmers only need to care about files that only contain PHP code and do not need to care about the appearance of the page; while page designers can use their own Design your page layout with the most familiar visual editor, without worrying about breaking any PHP code embedded into the page.
If you have watched a few tutorials on PHP templates, then you should already understand how templates work. Consider a simple page part: the top of the page is the header, the left is the navigation bar, and the rest is the content area. This kind of website can have the following template file:
Template Example title>
{HEADER} |
< ;td>{LEFTNAV}{CONTENT} |
FooBar You can see how the page is constructed from these templates: the main template controls the layout of the entire page; the header template and leftnav template control the common elements of the page. Identifiers inside curly braces "{}" are content placeholders. The main benefit of using templates is that interface designers can edit these files according to their own wishes, such as setting fonts, modifying colors and graphics, or completely changing the layout of the page. Interface designers can edit these pages with any ordinary HTML editor or visualization tool, because these files only contain HTML code, without any PHP code.
The PHP code is all saved into a separate file, which is the file actually called by the page URL. The web server parses the file through the PHP engine and returns the results to the browser. Generally, PHP code always dynamically generates page content, such as querying a database or performing certain calculations. Here is an example:
// example.php
require('class.FastTemplate.php');
$tpl = new FastTemplate('.') ;
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
'leftnav' => ' leftnav.htm' ) );
// The PHP code here sets $content to contain the appropriate page content
$tpl->assign('CONTENT', $content) ;
$tpl->parse('HEADER', 'header');
$tpl->parse('LEFTNAV', 'leftnav');
$tpl->parse(' MAIN', 'main');
$tpl->FastPrint('MAIN');
?>
Here we are using the popular FastTemplate template class, but The basic idea is the same for many other template classes. First, you instantiate a class and tell it where to find template files and which template file corresponds to which part of the page; then, generate the page content and assign the result to the content identifier; then, parse each template file in turn, The template class will perform the necessary replacement operations; finally, the parsing results will be output to the browser.
This file is entirely composed of PHP code and does not contain any HTML code, which is its biggest advantage. Now, PHP programmers can focus on writing the code that generates the content of the page, rather than worrying about how to generate the HTML to properly format the final page.
You can use this method and the above files to construct a complete website. If the PHP code generates page content based on the query string in the URL, such as http://www.foo.com/example.php?article=099, you can construct a complete magazine website based on this.
It’s easy to see that there is a second benefit to using templates. As shown in the example above, the navigation bar on the left side of the page is saved as a separate file. We only need to edit this template file to change the navigation bar on the left side of all pages of the website.
Avoid duplication of page elements
“This is really good”, you may be thinking, “My website is mainly composed of a large number of static pages.Now I can remove the public parts from all the pages, which would be too much trouble to update. In the future I can use templates to create a unified page layout that is easy to maintain. "But things are not that simple. "A large number of static pages" tells the problem.
Please consider the above example. This example actually only has one example.php page, and the reason why it can generate the entire website of all pages because it uses query strings in the URL to dynamically construct pages from information sources such as databases
Most of us run websites that don’t necessarily have that. Database support. Most of our websites are composed of static pages, and then we use PHP to add some dynamic functions here and there, such as search engines, feedback forms, etc. So, how to apply templates to this kind of website?
The simplest method is to copy a PHP file for each page, and then set the variables representing the content in the PHP code to the appropriate page content in each page. For example, suppose there are three pages, which are the homepage ( home), about (about) and product (product), we can use three files to generate them respectively. The contents of these three files are similar to:
/ / home.php
require('class.FastTemplate.php');
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
'leftnav' => 'leftnav.htm' ) );
$content = "< p>Welcome
Hope you like this website
";
$tpl- >assign('CONTENT', $content);
$tpl->parse('HEADER', 'header');
$tpl->parse('LEFTNAV', 'leftnav');
$tpl->parse('MAIN', 'main');
$tpl->FastPrint('MAIN');
?>
Obviously , there are three problems with this approach: we have to duplicate these complex, template-involved PHP codes for every page, which makes the page difficult to maintain as well as duplicating common page elements; now the files are mixed with HTML and PHP code; for the content Variable assignment will become very difficult because we have to deal with 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 the HTML content from the file, we can move out the vast majority of the PHP code.
Template framework for static websites
First, we write template files for all common elements of the page and the overall layout of the page as before; then delete the common parts from all pages, leaving only the page content; and then Add three lines of PHP code to each page, as follows:
Hello
< ;p>Welcome
Hope you like this website
< ;?php pageFinish(); ?>
?>
This method basically solves the various problems mentioned earlier. There are only three lines of PHP code in the file now, and none of them directly refer to the template, so the possibility of changing this code is extremely slim. In addition, since the HTML content is outside the PHP markup, there is no problem with handling special characters. We can easily add these three lines of PHP code to all static HTML pages.
The require function introduces a PHP file that contains all the necessary template-related PHP code. The pageStart function sets the template object and page title, and the pageFinish function parses the template and generates the result and sends it to the browser.
How is this achieved? Why is the HTML in the file not sent to the browser until the pageFinish function is called? The answer lies in a new feature of PHP 4, which allows the content output to the browser to be intercepted into a buffer. Let's take a look at the specific code of prepend.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 up a template instance, then enables output caching. After this, all HTML content from the page itself will go into the cache. The pageFinish function takes out the contents from the cache, then specifies these contents in the template object, and finally parses the template and outputs the completed page.
This is the entire working process of the entire template framework. First write a template that contains common elements for each page of the website, then delete all common page layout codes from all pages and replace them with three lines of PHP code that never need to be changed; then add the FastTemplate class file and prepend.php to the include path , so that you get a website whose page layout can be controlled centrally, which has better reliability and maintainability, and large-scale modifications at the website level become quite easy.
The download package for this article contains a runnable sample website, and its code comments are more detailed than the previous code comments. The FastTemplate class can be found at http://www.thewebmasters.net/, the latest version number is 1.1.0, and there is a small patch there to ensure that the class runs correctly in PHP 4. The classes in the download code of this article have been corrected by this patch.
http://www.bkjia.com/PHPjc/314011.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314011.htmlTechArticle Templates can improve the structure of the website. This article explains how to use a new function and template class in PHP 4 to skillfully use templates to control page layout in a website composed of a large number of static HTML pages...