Several often overlooked but useful tags in the PHP smarty template engine, including capture tags, config_load tags, php tags, strip tags, fetch tags, and how to use define-defined constants directly in Smarty templates , this article will analyze each of the following with specific examples:
Capture Tab
Capture's Chinese meaning is crawl, its role is to crawl the template output data, when we need it, call it to get the purpose of fetching data. Here's an example:
{Capture name= "test"} {/capture} {$smarty. Capture.test}
Description: The contents of {Capture Name= "test"} and {/capture} are stored in the variable $test, which is specified by the Name property. The variable is accessed by the $smarty. Capture.test in the template. If the Name property is not specified , the function defaults to use "default" as a parameter, which is similar to the Clone method in jquery.
Config_load Label
Config_load can read the contents of the file directly, thus eliminating the assign step, as in the following example:
Test.csv file:
PageTitle = "Config_load_test" Bodybgcolor = "#eeeeee" img = "girl.jpg" width= "100" height= "
The above Test.csv file can be referenced in the template Index.tpl file as follows:
{config_load file= "test.csv"}{#pageTitle #}
Note: If such a problem occurs in the above process Warning:smarty error:unable to read resource, please check that your test.csv is not placed in the Smarty configuration directory, the default configuration directory is the Configs directory.
PHP tags
When you get used to the assign, have you ever thought, in the template file directly to write PHP code, although very not recommended to do so, but sometimes forced by the business needs to do so when the need to do so swollen? Take a look at the following example:
{Php}global $result, foreach ($result as $key = + $value) { echo "key= $key,value=> $value
";} {/php}
Strip label
Strip the role of the label is to remove the space inside the label and enter, I feel this label is still very useful, you can compress the final output of the HTML format, want to see the effect of the words, see the source code on the site to know, is not very cool, hehe:
{Strip} Strip PHP smarty Strip compressed HTML output, www.phpernote.com see the source effect {/strip}
Fetch label
The fetch tag and PHP function file_get_contents function Almost, can read the contents of the file, and can be read out in the form of a string to assign a value to a variable, such as the following use case:
{Fetch file= "./aaaa.txt" assign= "Result"} {if Is_array ($result)} is array {Else If} Not array {/if}
Constants defined in PHP using define can be used directly in the Smarty template
Here's how to use it:
{$smarty. Const. The constant name you defined}
Articles you may be interested in
- How to use MVC patterns in PHP
- Eight classic tricks for Linux OS applications
- Smarty Variable operator Summary
- PHP 10 Very useful advanced application tips
- Smarty Template Retention Variables Summary
- Improve database performance uncover SQL optimization tips
- Summary of Smarty logical operation symbols
- Smarty include file methods for using variables
http://www.bkjia.com/PHPjc/764180.html www.bkjia.com true http://www.bkjia.com/PHPjc/764180.html techarticle The PHP smarty template engine has several often overlooked but more useful tabs to organize, including capture tags, config_load tags, php tags, strip tags, fetch tags, and how-to in s ...