Smarty a method for implementing page statics (generating HTML) _php instance

Source: Internet
Author: User
Tags php programming php template smarty template

The example in this article describes the Smarty implementation of page static (generate HTML) method. Share to everyone for your reference, specific as follows:

To reduce the number of database reads, some pages that are not frequently changed, such as the article detail page, need to be made into HTML static pages.

In the case of using smarty, you can also implement page statics. Let's say a little bit about the way you normally read dynamically when using Smarty.

Generally divided into these steps:

1. Pass a parameter (ID) by URL;

2, then query the database according to this ID;

3, after obtaining the data according to need to modify the display content;

4, assign need to display the data;

5, display template file.

The Smarty static process simply adds two steps to the process:

First: Use Ob_start () to open the buffer before 1.

Second: Use Ob_get_contents () after 5 to get the memory not output, and then use Fwrite () to write the content to the destination HTML file.

According to the above description, this process is implemented in the site foreground, and content management (add, modify, delete) is usually done in the background, in order to effectively

Using the above process, you can use a small means, that is header (). The specific process is this: after adding and modifying the program, use the

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

The process is not visible.

<?php
$cachefile = "./cache/demo.html";//put the cached file in a cache folder
$cachetime =20;
if (!file_exists ($cachefile) | | | filemtime ($cachefile) + $cachetime < Time ())//To determine the existence and expiration time
{
  ob_start ();// Output control
  echo ' <table border= "1" width= "align=" center ">";
  Echo ' <caption> 
 

More interested in smarty related content readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with your PHP programming based on Smarty templates.

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.