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.
(insert, if, elseif, else , ldelim, rdelim, literal, php, section, sectionelse, strip and other built-in functions, please refer to the built-in function 2 of php template engine smarty)
#capture function
config_load
foreach, foreachelse
include
include_php
capture
Attribute Name Type Required Default Description
name string no default The name of the captured block
assign string No n/a The variable name where to assign the captured output to
Is the attribute type required? Default value description
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]
The function of the capture function is to capture the data output by the template and store it in a variable instead of outputting them to the page.
Any The data between {capture name="foo"} and {/capture} will be stored in the variable $foo, which is specified by the name attribute.
Access this variable in the template through $smarty.capture.foo.
If the name attribute is not specified, the function will use "default" as the parameter by default.
{capture} must appear in pairs, that is, end with {/capture}, and the function cannot be nested.
Example:
{* This example is in After capturing the content, output a row of tables containing data. If no data is captured, nothing will be output*}
{capture name=banner}
{include file="get_banner.tpl"}
{/capture}
{if $smarty .capture.banner ne ""}
config_load
from Loading variables in the configuration file
Whether the attribute type must have a default value description
file string Yes n/a The name of the configuration file to be included
section string No n/a The name of the section to be loaded in the configuration file
scope string no local Loading data The scope of the variable 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 this variable is all templates.
global boolean No No Indicates whether the loaded variable is globally visible, equivalent to scope=parent. Note: When the scope attribute is specified, this attribute can be set, but the template ignores the attribute value and Subject to the scope attribute.
Example:
{config_load file="colors.conf"}
First | Last | Address |
Profile possible Contains multiple parts. At this time, you can use the additional attribute section to specify which part to get the variable from.
Note: The section in the configuration file and the template built-in function section are just named the same and have nothing to do with each other.
Example:
{config_load file="colors.conf" section="Customer"}
First< /td> | last | Address |
foreach ,foreachelse
foreach is another solution for processing loops besides section (choose different solutions according to different needs).
foreach is used to process simple arrays (the types of elements in the array are the same). Its format is much simpler than section. It has disadvantages It can only handle simple arrays.
foreach must be used in pairs with /foreach, and the from and item attributes must be specified.
name attributes can be specified arbitrarily (a combination of letters, numbers, and underscores).
foreach can be nested, but it must be guaranteed The foreach name in the nest is unique. The from attribute (usually an array) determines the number of loops. The foreachelse statement is executed when the from variable has no value. Whether the attribute type must have a default value Description
from string Yes n/a The name of the array to be looped
item string Yes n/a The variable name of the currently processed element
key string No n/a The key name of the currently processed element
name string No n/a The name of the loop, used to access the loop
Example 1:
{* This example will output the values of all elements in the array $custid*}
id: {$curr_id}
{/foreach}
Output result:
id: 1000
id: 1001
id: 1002
Example 2:
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
array("phone" => "555-4444", "fax" => "555-3333 ", "cell" => "760-1234")));
*}
{* The key is the subscript of the array, please refer to the explanation of the array*}
{foreach name=outer item=contact from=$ contacts}
{foreach key=key item=item from=$contact}
{$key}: {$item}
{/foreach}
{/foreach}
Output result:
phone: 1
fax: 2
cell: 3
phone: 555-4444
fax: 555-3333
cell: 760-1234
foreach loop has its own variables Name, use this variable name to access the loop. The usage method is {$smarty.foreach.foreachname.varname}, where foreachname is the name attribute specified in foreach. The
include
Is the attribute type required? Default value description
file string Yes n/a The name of the template file to be included
assign string No n/a This attribute specifies a variable to save the output of the template to be included
[var ...] [var type ] No n/a Local parameters passed to the template to be included are only valid in the template to be included
Example 1 include function demonstration
{include file="header.tpl"}
{* body of template goes here *}
Example 2 include function demonstration with passed parameters
{include file="header.tpl" title="Main Menu" table_bgcolor="#c0c0c0"}
{* body of template goes here *}
Example 3 Demonstration of include function using external template resources
{* absolute filepath * }
{include file="/usr/local/include/templates/header.tpl"}
{include file="file:/usr/local/include/templates/header .tpl"}
{* windows absolute filepath (MUST use "file:" prefix) *}
{include file="file:C:/www/pub/templates/header.tpl"}
{* include from template resource named "db" *}
{include file="db:header.tpl"}
include_php
inluce_php function is used to include php scripts in templates. If safe mode is set, the included script must be located in the $trusted_dir path. The include_php function must set the file attribute, which specifies the path to the included php file, which can be The relative path of $trusted_dir can also be an absolute path.
include_php is a good way to solve the problem of template componentization. It separates the PHP code from the template file. For example: Suppose there is a method to dynamically retrieve data from the database for For a template that displays site navigation, you can separate the php logic part of the data content and save it in a separate folder, and include the php script at the beginning of the template. Then you can include this template anywhere without worrying Whether the database information has been fetched by the program before.
Even if the php files are called multiple times in the template, they are only included once by default. You can set the once attribute to indicate that the file is re-included for each call. If once is If the attribute is set to false, the file will be re-included every time it is called.
If the assign attribute is set, the variable name corresponding to this attribute is used to save the output of the PHP file to be included, so that the output of the PHP file to be included will not be displayed directly. .
The smarty object can be accessed through $this in the php file to be included.
Whether the attribute type must have a default value description
file string Yes n/a The name of the php file to be included
once boolean No true If the php file to be included has been included Whether to still include (similar to the include_once function in php)
assign string No n/a This attribute specifies a variable to save the output of the php file to be included
Example
// load in variables from a mysql db and assign them to the template
// Get data from the mysql database and assign the data to the template variable require_once("MySQL.class.php");
$sql = new MySQL;
$sql->query("select * from site_nav_sections order by name",SQL_ALL);
$this->assign('sections',$sql->record);
?>
index.tpl
---------
{ * absolute path, or relative to $trusted_dir *}
{* Absolute path or relative path to $trusted_dir*}
{include_php file="/path/to/load_nav.php"}
{foreach item="curr_section" from= $sections}
{/foreach}
The above is the content of one of the built-in functions of PHP template engine smarty. For more related content, please pay attention to PHP Chinese website (www.php.cn)!