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>