Template Framework _php tutorial for making static websites with PHP

Source: Internet
Author: User
Templates can improve the structure of your site. This article explains how to use templates to control page layouts in a Web site composed of a large number of static HTML pages, using a new feature and template class for PHP 4.

Outline:

===================================

Separating features and layouts


Avoid duplicate page elements


Template framework for static web sites

===================================

Separating features and layouts

First, let's look at the two main purposes of applying a template:

Detach function (PHP) and layout (HTML)

Avoid duplicate page elements

The first is to talk about the most, and it assumes that a group of programmers write PHP scripts to generate page content, while another group of designers design HTML and graphics to control the final appearance of the page. The basic idea of separating functions and layouts is to enable the two groups of people to write and use separate sets of files: programmers only care about files that contain only PHP code, don't care about the appearance of the page, and page designers can design the page layout with their most familiar visual editor. No need to worry about breaking any PHP code embedded in the page.

If you've ever seen a few tutorials on PHP templates, you should already understand how templates work. Consider a simple page detail: The top of the page is the page header, the left side is the navigation bar, and the rest is the content area. This site can have the following template files:



<title>Template examples</title>



{HEADER}
{LeftNav} {CONTENT}








Foo

Bar

You can see how the pages are constructed from these templates: The main template controls the layout of the entire page, and the header template and the LeftNav template control the common elements of the page. The identifier inside the curly brace "{}" is a content placeholder. The main benefit of using templates is that the interface designer can edit the files as they wish, 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 visualizer, because they contain only HTML code and no PHP code. The PHP code is all saved to a separate file, which is the file that is actually called by the page URL. The Web server parses the file through the PHP engine and returns the results to the browser. In general, PHP code always dynamically generates page content, such as querying a database or performing some sort of calculation. Here is an example:

example.php
Require (' class. Fasttemplate.php ');
$TPL = new Fasttemplate ('. ');
$TPL->define (' main ' = ' main.htm ',
' Header ' = ' header.htm ',
' LeftNav ' = ' leftnav.htm ');

The PHP code settings here $content to include 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, tell it where to look for the template file and which template file corresponds to which part of the page, then generate the page content, assign the result to the identifier of the content, and then parse each template file in turn, the template class will perform the necessary substitution operations, and finally output the parsing results to the browser.

This file is completely composed of PHP code, does not contain any HTML code, this is its biggest advantage. Now, PHP programmers can focus on writing code that generates page content, rather than worrying about how to generate HTML to properly format the final page.

You can use this method and the above file to construct a complete Web site. 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 full magazine website accordingly.

It is easy to see that there is a second benefit to adopting a template. As shown in the example above, the navigation bar on the left side of the page is saved separately as a file, and we can simply edit this template file to change the navigation bar on the left side of all pages of the site. Avoid duplicate page elements
"It's really good," You might think, "My site is mostly made up of a lot of static pages." Now that I can remove the public parts from all the pages, it's too much trouble to update the public parts. Later I can use the template to create a very easy to maintain a unified page layout. "But it's not that simple," a lot of static pages "are going to be the problem," he said.

Please consider the above example. This example actually has only one example.php page, it can generate all the pages of the whole site, because it takes advantage of the query string in the URL to dynamically construct the page from information sources such as databases.

Most of us run websites that don't necessarily have database support. Most of our sites are made up of static pages, and then we use PHP to add dynamic features, such as search engines, feedback forms, and so on. So, how to apply a template on such a website?

The simplest way to do this is to copy a PHP file for each page, and then set the variable in the PHP code to the appropriate page content on each page. For example, suppose you have three pages, which are home, about, and product, and we can generate them in three separate files. The contents of these three files are classes such as:


home.php
Require (' class. Fasttemplate.php ');
$TPL = new Fasttemplate ('. ');
$TPL->define (' main ' = ' main.htm ',
' Header ' = ' header.htm ',
' LeftNav ' = ' leftnav.htm ');

$content = "

Welcome to visit



Hope you can 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 replicate these complex, template-related PHP code for each page, which makes the page difficult to maintain, just like repeating common page elements, and now the files are mixed with HTML and PHP code, and assigning values to content variables becomes 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 from the HTML content, although we can't remove all the HTML content from the file, but we can move out of the vast majority of PHP code. Template framework for static web sites

First, we write the template file for all the common elements of the page and the overall layout of the page as before, then delete the public section from all the pages, leaving only the page content, and then add three lines of PHP code to each page, as follows:






How are you doing


Welcome to visit



Hope you can like this website





?>

This approach basically solves the various problems mentioned earlier. Now there are only three lines of PHP code in the file, and no line of code directly touches the template, so it is very unlikely to change the code. In addition, because the HTML content is outside the PHP tag, there is no processing problem with 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 PHP code related to the template. Where the Pagestart function sets the template object as well as the page title, the Pagefinish function resolves the template and generates the result to send to the browser.

How is this implemented? Why is the HTML in the file not sent to the browser before calling the Pagefinish function? The answer lies in a new PHP 4 feature that allows the content of the output to the browser to be intercepted into the buffer. Let's take a look at the specific code for prepend.php:


Require (' class. Fasttemplate.php ');

function Pagestart ($title = ") {
GLOBAL $tpl;
$TPL = new Fasttemplate ('. ');
$TPL->define (' 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 and then enables output caching. Thereafter, all HTML content from the page itself is entered into the cache. The Pagefinish function takes out the contents of the cache and then specifies the contents in the template object, finally parsing the template and outputting the finished page.
This is the whole process of working with the template framework. Start by writing a template that contains the common elements of each page of the site, and then remove all common page layout codes from all pages, replacing them with three lines of PHP code that never needs to be changed and add the Fasttemplate class files and prepend.php to the include path, so you get a Web site with a page layout that can be centrally controlled, with better reliability and maintainability, and a wide range of site-wide modifications becomes quite easy.

This download package contains a sample Web site that can be run, with code comments that are more detailed than the previous code comments. The Fasttemplate class can be found in http://www.thewebmasters.net/, the latest version number is 1.1.0, and there is a small patch to keep the class running correctly in PHP 4. The class in the download code for this article has been modified by the patch.

http://www.bkjia.com/PHPjc/314011.html www.bkjia.com true http://www.bkjia.com/PHPjc/314011.html techarticle templates can improve the structure of your site. This article explains how to use templates to control page layouts in a Web site composed of a large number of static HTML pages, using a new feature and template class for PHP 4 .

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.