Smarty method of implementing page static (HTML generation), Smarty static
The example in this article describes how Smarty implements the static page (HTML generation). Share to everyone for your reference, as follows:
In order to reduce the number of database reads, some of the content is not often changed pages, such as the article detail page needs to be made into HTML static pages.
In the case of using smarty, you can also implement page statics. Let's start with a brief look at the usual dynamic reading when using Smarty.
These steps are generally divided into:
1. Pass a parameter (ID) via URL;
2, then query the database based on this ID;
3, after obtaining the data, modify the display content as needed;
4, assign need to display the data;
5. Display template file.
The Smarty static process requires only two steps to be added in the above procedure:
First: Use Ob_start () to open the buffer before 1.
Second: After 5, use Ob_get_contents () to get the contents of the memory not output, and then use Fwrite () to write the content to the destination HTML file.
As described above, this process is implemented in the foreground of the website, and content management (add, modify, delete) is usually done in the background in order to effectively
Using the above procedure, you can use a small method, that is, the header (). The process is as follows: After adding and modifying a program, use the
Header () (and, of course, other ways) jumps to the foreground to read, which enables the HTML of the page, and then jumps back to the admin side after the HTML is generated, and these two jumps
The process is not visible.
<?php$cachefile= "./cache/demo.html";//put the cache file in a folder $cachetime=20;if (!file_exists ($cachefile) | | Filemtime ($cachefile) + $cachetime < Time ())//determine presence and expiration { ob_start ();//Output Control echo '
'; Echo '
User
'; Echo '
'; echo "
| 11111 | "; echo "
22222 | "; Echo '
'; Echo '
'; echo "
| 11111 | "; echo "
22222 | "; Echo '
'; Echo '
'; $html =ob_get_contents (); File_put_contents ($cachefile, $html);//output to cache file Ob_end_flush ();//output and close buffer}else{ Echo ' Ceshi '; Include $cachefile;}? >
More interested in smarty related content readers can view the topic: "Smarty Template Primer Basic Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " PHP Basic Grammar Introductory tutorial, PHP Object-oriented Programming primer, PHP string usage Summary, PHP+MYSQL database Operations Primer and PHP Common database operations Tips Summary
It is hoped that this article will be helpful to everyone based on smarty template PHP program design.
http://www.bkjia.com/PHPjc/1133094.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133094.html techarticle Smarty Implement the method of page static (generate HTML), Smarty static This article describes the method smarty implement the page static (generate HTML). Share to everyone for your reference, as follows: ...