This article describes the built-in functions of the PHP template engine Smarty. Share it for your reference, as follows:
Smarty's built-in functions: Smarty comes with some built-in functions. Built-in functions are part of the template language. Users cannot create custom functions with the same name as built-in functions, nor can they modify built-in functions.
The following is an example of the built-in functions in Smarty:
Smarty template engine initialization files and main files used in the instance
<?php define('ROOT_PATH', dirname(__FILE__)); //Set the website root directory require ROOT_PATH.'/libs/'; //Load the Smarty template engine $_tpl = new Smarty(); //Create an instance object $_tpl->template_dir = ROOT_PATH.'/tpl/'; //Re-specify the template directory $_tpl->compile_dir = ROOT_PATH.'./com/'; //Respecify the compiled directory $_tpl->left_delimiter = '<{'; //Respecify the left delimiter $_tpl->right_delimiter = '}>'; //Respecify the right delimiter?>
<?php require ''; //Introduce template initialization file global $_tpl; $_tpl->display(''); //Introduce template?>
1、capture
property | type | Is it necessary | Default value | describe |
---|---|---|---|---|
name | string | no | default | Data collection area name |
assign | string | No | n/a | Where is the data collection area assigned to the variable name [to be tested] |
/tpl/
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Capture</title> </head> <body> <!-- definitioncapture --> <{capture name="foo"}> here it is capture Contents in the function,It is not displayed by default。 <{/capture}> <!-- Callcapture,Used Smarty Reserved variables in{$} --> <{$}> </body> </html>
2、config_load
property | type | Is it necessary | Default value | describe |
---|---|---|---|---|
file | string | Yes | n/a | The name of the configuration file to be included |
section | string | No | n/a | Name of the part to be loaded in the configuration file |
scope | string | no | local | The scope of the data loading must be local, parent or global. local indicates that the scope of the variable is the current template. parent indicates that the scope of the variable is the current template and the parent template of the current template (the template that calls the current template). global indicates that the scope of the variable is all templates. |
global | boolean | No | No | Indicates whether the loaded variable is globally visible, which is equivalent to scope=parent. Note: When the scope property is specified, the property can be set, but the template ignores the property value and takes the scope property as the basis. |
3、include
property | type | Is it necessary | Default value | describe |
---|---|---|---|---|
file | string | Yes | n/a | The template file name to be included |
assign | string | No | n/a | This property specifies a variable to save the output of the template to be included |
[var ...] | [var type] | No | n/a | The local parameters passed to the template to be included are valid only in the template to be included |
The include function is used to include other templates in the current template. Variables in the current template are available in the included template. The file attribute must be specified, which indicates the location of the template resource. If the assign property is set, the variable name corresponding to the property is used to save the output of the template to be included, so that the output of the template to be included will not be displayed directly. Please see the following example:
/tpl/
{include file=""} {* body of template goes here *} {include file=""}
4、if,elseif,else
The if statement in Smarty is as flexible and easy to use as the if statement in php, and several features are added to suit the template engine. If must appear in pairs in /if. The else and elseif clauses can be used.
The following conditional modifiers can be used: eq, ne, neq, gt, lt, lte, le, gte, ge, is even, is odd, is not even, is not odd, not, mod, div by, even by, odd by, ==, !=, >, <, <=, >=. When using these modifiers, you must be spaced with variables or constants.
The following explains the meaning of these modifiers:
Conditional modifier | Description of the function |
eq | == |
ne | != |
neq | != |
gt | > |
lt | < |
lte | <= |
le | <= |
gte | >= |
ge | >= |
is even | Is it even? |
is odd | Is it odd? |
is not even | Is it not an even number |
is not odd | Isn't it an odd number |
not | != |
mod | Seek model |
div by | Can it be removed |
even by | Is the quotient even number |
odd by | Is the quotient an odd number |
&& | and |
|| | or |
() | Brackets change priority |
5. ldelim and rdelim
Used to output delimiters, i.e. braces "{" and "}". The template engine always tries to interpret what's inside braces, so use this method if you need to output braces. Please see the following example:
/tpl/
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ldelim and rdelim</title> </head> <body> <{ldelim}>funcname<{rdelim}> yes Smarty A function in。 <!-- Execution results: <{funcname}> yes Smarty A function in。 --> </body> </html>
6、literal
The data in the literal tag area will be processed as text, and the template will ignore all character information inside it. This feature is used to display javascript scripts that may contain braces and other character information. When this information is in the {literal}{/literal} tag, the template engine will not analyze them, but will display it directly, in fact, in the label style in all my examples (because the left and right delimiters have been reset in the initialization file), rather than the default style of Smarty, this situation will not occur. Regarding the use of this function, please see the following example
/tpl/
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>literal</title> </head> <body> <{literal}> <script language=javascript> <!-- (new Date()); --> </script> <{/literal}> </body> </html>
7、php
The php tag allows the php script to be directly embedded in the template. This tag will parse and execute the contents inside the tag as PHP scripts. Please see the following example
/tpl/
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php</title> </head> <body> <{php}> echo date("Y-m-d H:i:s"); <{/php}> <!-- Execution results: 2011-10-24 04:35:03 --> </body> </html>
8、strip
Web developers encounter spaces and carriage returns that affect HTML output multiple times. In order to get specific results, you have to run all tags in the template. This problem is usually encountered in templates that are difficult to understand or difficult to handle. Smarty will remove any data in the {strip}{/strip} tag before displaying. This ensures that the template is easy to understand and there is no need to worry about the problem of unnecessary spaces.
Okay, so many built-in functions in the Smarty template engine are summarized first. For the use of the two most important functions (foreach, foreachelse, section, sectionelse) in the built-in function, please refer to the previous article "PHP template engine Smarty built-in function foreach, foreachelse usage analysis》
For more information about PHP related content, please check out the topic of this site:Basic tutorial on getting started with smarty templates》、《PHP template technical summary》、《Summary of PHP's skills to operate database based on pdo》、《Summary of PHP operations and operator usage》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope that this article will be helpful to everyone's PHP programming based on smarty templates.