


Smarty's method of staticizing pages (generating HTML), smarty static_PHP tutorial
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.
<?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; } ?>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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

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.
