Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement smarty in php

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to achieve smarty in php, which is very detailed and has a certain reference value. Interested friends must read it!

In PHP, smarty is a template engine written in PHP, which provides an easy-to-manage and easy-to-use method to logically separate the PHP code originally mixed with HTML code, so as to improve the speed of programs written in smarty.

This article operating environment: Windows10 system, PHP7.1 version, Dell G3 computer

Smarty concept: what is Smarty?

Smarty is a template engine written in PHP language, and it is one of the most famous PHP template engines in the industry. It separates logical code from external content, and provides an easy-to-manage and easy-to-use way to logically separate PHP code that was originally mixed with HTML code. To put it simply, the purpose is to separate the PHP programmer from the front-end staff, so that the programmer changes the logical content of the program will not affect the front-end staff's page design, and the front-end staff re-modify the page will not affect the program logic, which is particularly important in multi-person cooperation projects.

What are the characteristics of Smarty?

1. Fast (because the second execution uses the compiled file generated on the first execution)

2. Caching technology (it is because of caching technology that smarty template technology is not suitable for those with high requirements for real-time updates, such as stock information)

3. Plug-in technology

4. Caching technology

5. Statement free static page technology is actually a technology of exchanging space for time.

Smarty object-oriented implementation template function encapsulation:

/ / define Smarty class class Smarty {/ * Save template corresponding tag data * @ var array*/private $tplData = array (); / * set data for template tag attributes * @ param sting $key corresponding template tag signature * @ param mixed $val corresponding template tag value * @ return*/public function assign ($key, $val) {$this- > tplData [$key] = $val } / * generate compilation file * @ param string $tpl template file (name + suffix) * @ return*/public function display ($tpl) {/ / generate compilation file / / or / / template file last modification time > build file creation time regenerate compilation file if (! file_exists ("$tpl.php") | | filemtime ($tpl) > filemtime ("$tpl.php") {/ / 1. Get the template file data $htmlData = file_get_contents ($tpl); / / 2. Replace template tag / / replace {$title} / /

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report