Home  >  Article  >  Backend Development  >  php smarty template engine_PHP tutorial

php smarty template engine_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:08:07727browse

PHP is an embedded HTML scripting language. In order to separate HTML and PHP code, the so-called logic layer and presentation layer, this is the purpose of the template engine. In order to achieve this goal, the template engine needs to have the following functions:

1. Storage variables;
2. Read the template file;
3. Combine the first two to generate output.
Code is as follows:
test01.php
$name='xiaoshenge';
?>
test02.php
test
name=
test03.php
include'test01.php';
include'test02.php;
?>
Of course, this is just a simple simulation of how to implement the functions of the PHP template engine. The famous smarty in the PHP open source community encapsulates the above functions. Save data->Load template->Compile and generate output file. Please refer to the manual for specific applications of smarty. Here we only explain its functional principles.
Record the problems encountered by smarty cache.
If smarty cache is enabled, the compiled output file will be saved in the cache directory when executed for the first time. In the program, smarty’s is_cache() function is used to detect whether the cache file has expired. If it expires, the cache will be updated. If it has not expired, the cache file will be automatically called, eliminating the compilation process. Detecting cache expiration is to see whether the template file has changed within the specified life cycle. The change here is achieved by detecting the latest modification time of the file, not by detecting the content of the template file.
Prevent the entire template file from being cached:
index.php:
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->caching = true;
function smarty_block_dynamic($param, $content, &$smarty) {
return $content;
}
$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
$smarty->display('index.tpl');
index.tpl:
Page created: {"0"|date_format:"%D %H:%M:%S"}
{dynamic}
Now is: {"0"|date_format:"%D %H:%M:%S"}
... do other stuff ...
{/dynamic}
When reloading this page, you will notice that the two dates are different. One is "dynamic" and the other is "static". You can do anything between {dynamic}...{/dynamic} and be sure it won't be cached like the rest of the page.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477813.htmlTechArticlephp is an embedded HTML scripting language. In order to separate HTML and php code, the so-called logic layer and presentation layer , which is what template engines are for. In order to achieve this purpose, the template engine needs to have...
Statement:
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