PHP smarty Template engine

Source: Internet
Author: User
Tags php template smarty template
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.


  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.