PHP's Smarty template engine
PHP is an embedded HTML scripting language, in order to separate the HTML and PHP code, the so-called logical layer and the presentation layer, which is the purpose of the template engine. In order to achieve this purpose the template engine needs to have the function:
1. Storage variables;
2. Read the template file;
3. Combine the first two generated outputs.
Code is as follows:
test01.php
$name = ' Xiaoshenge ';
?>
test02.php
<title>Test</title>
Name=
test03.php
Include ' test01.php ';
Include ' test02.php;
?>
Of course, here is just a simple simulation of how to implement the PHP template engine function, in the PHP open source community, the famous smarty is encapsulated the above features. Save data, load template, compile build output file. For a specific reference manual for the application of smarty, here is simply an explanation of its functional principle.
Log the problem that the Smarty cache encounters.
If the Smarty cache is turned on, the first execution will save its compiled output file to the cache directory, in the program through the Smarty Is_cache () function to detect whether its cache file expires, if the expiration will update the cache, if not expired will automatically call the cache file, Eliminate the process of compiling. Detecting cache expiration is to see if the template file has changed over the specified life cycle, where the change is achieved by detecting the file's last modified time, not by detecting the template file contents.
Block the entire article of a 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"}
... does other stuff ...
{/dynamic}
When you reload this page, you will notice that the two dates are different. One is "dynamic" and one is "static". You can be in {dynamic} ... Do anything between {/dynamic} and make sure it won't be cached like the rest of the page.