Table of Contents
Smarty’s method of staticizing pages (generating HTML), smarty static
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

Jul 12, 2016 am 08:50 AM
smarty Static page

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;>

Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP and Smarty to achieve front-end and back-end separation development How to use PHP and Smarty to achieve front-end and back-end separation development Jun 25, 2023 pm 01:46 PM

In modern web development, the separation of front-end and back-end has become a very popular trend, which allows developers to better organize projects and improve the efficiency of project development. PHP and Smarty are two very commonly used technologies, which can be used to achieve front-end and back-end separation development. This article will introduce how to use PHP and Smarty to achieve front-end and back-end separation development. What is front-end and back-end separation development? In traditional web development, the front-end is mainly responsible for the presentation of the page and the logic of interaction with the back-end. The backend is mainly responsible for the business

A preliminary study on the template engine Smarty in PHP A preliminary study on the template engine Smarty in PHP May 11, 2023 pm 05:15 PM

Nowadays, website development is inseparable from an important component-template engine. A template engine refers to a tool that combines page templates and data to generate HTML code with a specific format. In various website development frameworks, the template engine is an essential component, because the template engine can greatly reduce the duplication of code and improve the dynamics of the page. One of the most common and popular template engines is Smarty. Smarty is a DSL (DomainSpecif

CodeIgniter middleware: Optimization strategy for fast caching and page staticization CodeIgniter middleware: Optimization strategy for fast caching and page staticization Jul 28, 2023 pm 03:54 PM

CodeIgniter Middleware: Optimization Strategies for Fast Caching and Page Staticization Introduction: Performance optimization has always been an important topic during the development of a website or application. In order to improve the response speed of the website and reduce the number of database accesses, we can use middleware to implement optimization strategies for fast caching and page staticization. This article will introduce how to use the middleware function of the CodeIgniter framework to implement these optimization strategies and provide corresponding code examples. 1. Overview of middleware Middleware is a kind of

Page staticization and cache update strategies in PHP flash kill system Page staticization and cache update strategies in PHP flash kill system Sep 19, 2023 am 10:37 AM

Page staticization and cache update strategies in the PHP flash sale system With the rapid development of the Internet and the continued increase in the number of users, flash sale activities are becoming more and more popular on e-commerce platforms. However, a large number of users accessing the flash sale page at the same time will put huge load pressure on the server, causing system crashes or long response times. In order to solve this problem, page staticization and cache update have become common optimization strategies in the PHP flash sale system. This article will introduce how to apply page staticization and cache update strategies in the PHP flash sale system to improve the performance and availability of the system.

How to use Smarty template engine in PHP development How to use Smarty template engine in PHP development Jun 27, 2023 pm 01:28 PM

As a PHP developer, using a template engine is a natural choice. Smarty is a popular template engine that provides a way to separate HTML/CSS/JavaScript from PHP code, allowing developers to better organize and manage projects. In this article, we will learn how to use Smarty template engine during PHP development. 1. Install Smarty Before, we must install Smarty. In this article we will use Composer to install

How to use PHP and the Smarty template engine How to use PHP and the Smarty template engine May 11, 2023 pm 03:33 PM

PHP is a powerful server-side scripting language that can be used to develop web applications. In the early days of web development, programmers used a lot of HTML and JavaScript code to develop web applications. However, this approach is difficult to maintain and manage because the HTML and JavaScript code can become very complex. To solve this problem, the Smarty template engine was created. Smarty is a template engine developed based on PHP for managing and generating W

How to use Smarty with CakePHP? How to use Smarty with CakePHP? Jun 03, 2023 pm 03:10 PM

CakePHP is an open source PHP framework that provides rich features and tools to accelerate web application development. One of the powerful features is the template engine. By default, CakePHP uses PHP's native syntax for view rendering. However, sometimes we may want to use another template engine, such as Smarty. This article will introduce how to use Smarty in CakePHP. 1. What is Smarty? Smarty is a template-based PHP framework that

what are thinkphp and smarty what are thinkphp and smarty Jun 14, 2022 pm 05:56 PM

thinkphp is an open source lightweight PHP framework that is used to simplify enterprise application development and agile WEB application development; using ThinkPHP, developers can develop and deploy applications more conveniently and quickly. Smarty is a PHP template engine that can better help developers separate program logic and page display (separation of business logic and display logic), so that programmers can change the logic content of the program without affecting the page design of the front-end staff, and the front-end staff can re- Modifying the page will not affect the program logic of the program.

See all articles