Home > Backend Development > PHP Tutorial > Using PHP to create a template framework for static websites (3)_PHP Tutorial

Using PHP to create a template framework for static websites (3)_PHP Tutorial

WBOY
Release: 2016-07-21 16:06:28
Original
849 people have browsed it

Avoid duplication of page elements


“This is really good”, you may be thinking, “My website is mainly made up of a lot of static pages. Now I can remove the common parts of them from all the pages, It's too troublesome to update these common parts. In the future, I can use templates to create a unified page layout that is easy to maintain. "But it is not that simple, and the "large number of static pages" reveals the problem.


Consider the example above. This example actually only has one example.php page. The reason why it can generate all pages of the entire website is that it uses the query string in the URL to dynamically construct pages from information sources such as databases.


Most of us run websites that don’t necessarily have database support. Most of our website is composed of static pages, and then PHP is used to add some dynamic functions here and there, such as search engines, feedback forms, etc. So, how to apply templates on 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, namely home, about, and 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 = "

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 must create a new page for each page Duplicating this complex PHP code involving templates makes the page difficult to maintain as well as duplicating common page elements; now the files are mixed with HTML and PHP code; assigning values ​​to content variables will become very difficult because we have to deal with a large number 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315434.htmlTechArticleAvoid duplication of page elements "This is really good", you may be thinking, "My website is mainly composed of a large number of Static page composition Now I can remove their common parts from all pages...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template