Home >Backend Development >PHP Tutorial >Smarty's method of staticizing pages (generating HTML), smarty static_PHP tutorial

Smarty's method of staticizing pages (generating HTML), smarty static_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:50:42952browse

Smarty’s method of staticizing pages (generating HTML), smarty static

This article describes the example of Smarty’s method of staticizing pages (generating HTML). Share it with everyone for your reference, the details are as follows:

In order to reduce the number of database reads, some pages whose content is not frequently changed, such as article details pages, need to be made into HTML static pages.

When using Smarty, the page can also be made static. Let's briefly talk about the usual dynamic reading method when using Smarty.

Generally divided into these steps:

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

2. Then query the database based on this ID;

3. Modify the display content as needed after obtaining the data;

4. assign the data to be displayed;

5. display template file.

The Smarty staticization process only requires adding two steps to the above process:

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

Second: Use ob_get_contents() after 5 to get the memory unoutput content, and then use fwrite() to write the content to the target html file.

According to the above description, this process is implemented in the front-end of the website, while content management (adding, modifying, deleting) is usually performed in the background. In order to be effective

Using the above process, you can use a little trick, that is Header(). The specific process is as follows: After adding and modifying the program, use

Header() (of course there are other ways) jumps to the foreground to read, so that the page can be HTMLized, and then jumps back to the background management side after generating the html, and these two jumps

The process is invisible.

<&#63;php
$cachefile="./cache/demo.html";//把缓存文件放到一个cache文件夹里
$cachetime=20;
if (!file_exists($cachefile ) || filemtime($cachefile)+$cachetime < time()) //判断是否存在和过期时间
{
  ob_start();//输出控制
  echo '<table border="1" width="800" align="center">';
  echo '<caption><h1>user</h1></caption>';
  echo '<tr>';
  echo "<td>11111</td>";
  echo "<td>22222</td>";
  echo '</tr>';
  echo '<tr>';
  echo "<td>11111</td>";
  echo "<td>22222</td>";
  echo '</tr>';
  echo '</table>';
$html=ob_get_contents();
file_put_contents($cachefile, $html);//输出到缓存文件
ob_end_flush();//输出并关闭缓冲区
}
else{
  echo 'ceshi';
 include $cachefile;
}
&#63;>

Readers who are interested in more Smarty-related content can check out the special topics on this site: "Basic Tutorial for Getting Started with Smarty Templates", "Summary of PHP Template Technology", "Summary of PHP Database Operation Skills Based on PDO", "PHP Operations and Operators" Usage summary", "PHP network programming skills summary", "PHP basic syntax introductory tutorial", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "Summary of Common Database Operation Skills in PHP"

I hope this article will be helpful to everyone’s PHP program design based on smarty templates.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133094.htmlTechArticleSmarty’s method of realizing page staticization (generating HTML), smarty static This article describes how Smarty realizes page staticization ( Generate HTML) method. Share it with everyone for your reference, the details are as follows:...
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