Smarty implements Static Page (HTML generation), and smarty static

Source: Internet
Author: User

Smarty implements Static Page (HTML generation), and smarty static

This article describes how to use Smarty to achieve Static Page (generate HTML. We will share this with you for your reference. The details are as follows:

To reduce the number of database reads, some pages whose content is not frequently changed, such as the detailed page of the article, must be HTML static pages.

When Smarty is used, the page can also be static. The following describes how to use Smarty to dynamically read data.

Generally, the following steps are taken:

1. Pass a parameter (ID) through the URL );

2. query the database based on this ID;

3. After obtaining the data, modify the display content as needed;

4. Data to be displayed for assign;

5. display template file.

In the static process of Smarty, you only need to add two steps in the above process:

First, use ob_start () to open the buffer before 1.

Second, use ob_get_contents () after 5 to get the content not output in the memory, and then use fwrite () to write the content to the target html file.

According to the above description, this process is implemented on the website front-end, and content management (ADD, modify, delete) is usually performed in the background, in order to be effective

Using the above process, you can use a small method, that is, Header (). The specific process is as follows: Use

Header () (of course there are other ways) Jump to the foreground to read, this can achieve page HTML, and then jump back to the background management side after generating html, and the two jump

The process is invisible.

<? Php $ cachefile = "./cache/demo.html"; // put the cached file in a cache folder $ cachetime = 20; if (! File_exists ($ cachefile) | filemtime ($ cachefile) + $ cachetime <time () // you can specify whether or not the file exists and the expiration time {ob_start (); // Output Control echo '<table border = "1" width = "800" align = "center"> '; echo '<caption> 

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.