Home  >  Article  >  Backend Development  >  Teach you how to use PHP to create a template framework for a static website_PHP Tutorial

Teach you how to use PHP to create a template framework for a static website_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 11:04:22684browse

The first purpose is the most talked about, and it envisions a situation where a group of programmers write the PHP scripts that generate the content of the page, while another group of designers designs the HTML and graphics to control the final appearance of the page. 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.

You can see how the page is constructed from 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 in a separate file. This file 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:

// 此处的PHP代码设置$content<br>使其包含合适的页面内容<br>$tpl->assign('CONTENT', $content); <br>$tpl->parse('HEADER', 'header'); <br>$tpl->parse('LEFTNAV', 'leftnav'); <br>$tpl->parse('MAIN', 'main'); <br>$tpl->FastPrint('MAIN'); <br>?>

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. This 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.

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445224.htmlTechArticleThe first purpose is the most talked about purpose. It envisages a situation where a group of programmers write a program for PHP scripts that generate page content while another team of designers designs the HTML and graphics to control...
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