Home  >  Article  >  Backend Development  >  Template framework for making static websites with PHP 3_PHP tutorial

Template framework for making static websites with PHP 3_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:28:42702browse

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 their common parts from all pages, and it is very difficult to update these common parts. It’s too troublesome. 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” reveals the problem.
Consider the above example. 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 to visit
Template framework for making static websites with PHP 3_PHP tutorial
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 this complex, template-involved PHP code for every page, which makes the page difficult to maintain as well as duplicating common page elements; now the file is a mix of HTML and PHP code; for 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531763.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...
Statement:
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