One of the built-in functions of the php template engine smarty

Source: Internet
Author: User
Tags key string php template
Abstract: you cannot create custom functions with the same name as built-in functions, or modify built-in functions... some built-in functions are included in Smarty.
Built-in functions are part of the template language.
You cannot create or modify a UDF with the same name as a built-in function.
(Insert, if, elseif, else, ldelim, rdelim, literal, php, section, sectionelse, strip, and other built-in functions can be found in the second built-in function 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

Whether the property type must be the default value description
Name string no default data collection region name
Assign string No n/a: where the data collection region is allocated to the variable name [to be tested]

The capture function captures the data output from the template and stores the data in a variable instead of outputting the data to the page.
Any data between {capture name = "foo"} and {/capture} will be stored in the variable $ foo, which is specified by the name attribute.
Access the variable through $ smarty. capture. foo in the template.
If the name attribute is not specified, the function uses "default" as the parameter by default.
{Capture} must appear in pairs, that is, end with {/capture}. this function cannot be nested.
Example:
{* This example outputs a table containing a row of data after the captured content. if no data is captured, nothing is output *}
{Capture name = banner}
{Include file = "get_banner.tpl "}
{/Capture}
{If $ smarty. capture. banner ne ""}


{$ Smarty. capture. banner}


{/If}


Config_load
Load variables from the configuration file
Whether the property type must be the default value description
File string Yes n/a 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: scope of the data to be loaded. The value must be local, parent, or global. local indicates that the variable scope is the current template. parent indicates that the variable scope is the current template and the parent template of the current template (call the template of the current template ). global indicates that the scope of the variable is all templates.
Global boolean No indicates whether the loaded variables are globally visible, equivalent to scope = parent. note: When the scope attribute is specified, you can set this attribute, but the template ignores this attribute and the scope attribute prevails.


Example:
{Config_load file = "colors. conf "}

{# PageTitle #}







First Last Address


The configuration file may contain multiple parts. in this case, you can use the additional property section to specify the part from which the variable is obtained.
Note: The section in the configuration file is the same as the section in the built-in function of the template, and it is irrelevant.
Example:
{Config_load file = "colors. conf" section = "Customer "}

{# PageTitle #}







First Last Address


Foreach, foreachelse

Foreach is another solution for processing loops except section (different solutions are selected based on different needs ).
Foreach is used to process a simple array (the element types in the array are the same). Its format is much simpler than that of section. The disadvantage is that it can only process a simple array.
Foreach must be used in pairs with/foreach, and the from and item attributes must be specified.
The name attribute can be arbitrarily specified (a combination of letters, numbers, and underscores ).
Foreach can be nested, but it must be unique in the nested foreach name.
The from attribute (usually an array) determines the number of cycles.
The foreachelse statement is executed when the from variable has no value.
Whether the property type must be the default value description
From string Yes n/a name of the array to be cyclic
Item string Yes n/a variable name of the currently processed element
Key string No n/a key name of the currently processed element

Name string No n/a name of the loop, used to access the cycle

Example 1:
{* This example outputs the values of all elements in the array $ custid *}
{Foreach from = $ custid item = curr_id}
Id: {$ curr_id}

{/Foreach}
Output result:
Id: 1000

Id: 1001

Id: 1002

Example 2:
$ Smarty-> assign ("contacts", 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 interpretation 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

The foreach loop has its own variable name, which can be used to access the loop. usage: {$ smarty. foreach. foreachname. varname}, where foreachname is the name attribute specified in foreach.
Include
The Include tag is used to Include other templates in the current template. the variables in the current template are available in the included template. you must specify the file attribute, which specifies the Template resource location.
If the assign attribute is set, the variable name corresponding to this attribute 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.
Whether the property type must be the default value description
File string Yes n/a template file name 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 is passed to the local parameter of the template to be included. it is valid only in the template to be included.

Example 1 include function demonstration
{Include file = "header. tpl "}
{* Body of template goes here *}
{Include file = "footer. tpl "}

Example 2 include function demonstration with passing parameters
{Include file = "header. tpl" title = "Main Menu" table_bgcolor = "# c0c0c0 "}
{* Body of template goes here *}
{Include file = "footer. tpl" logo = "http://www.php118.com/php118.gif "}

Example 3 use the include function of external template resources
{* Absolute filepath *}
{Include file = "/usr/local/include/templates/header. tpl "}
{* Absolute filepath (same thing )*}
{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
The inluce_php function is used to include php scripts in the template. if security mode is set, the included script must be in the $ trusted_dir path. the file attribute must be set for the include_php function. this attribute specifies the path of the included php file, which can be the relative path of $ trusted_dir or the absolute path.
Include_php is a good solution to template componentization. it isolates php code from the template file. for example, if there is a template that dynamically extracts data from the database for displaying site navigation, you can separate the php logic part of the data content and save it in a separate folder, the php script is included at the beginning of the template. you can include this template anywhere without worrying about whether the database information has been retrieved by the program.
Even if php files are called multiple times in the template, they are included only once by default. you can set the once attribute to indicate that the file is recontained in each call. if you set the once attribute to false, the file will be re-contained every time you call the file.
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.
You can use $ this to access the smarty object in the php file to be included.
Whether the property type must be the default value description
File string Yes n/a name of the PHP file to be included
Once boolean No true if the php file to be included is included or not (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
// Obtain 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 ('sets', $ SQL-> record );
?>

Index. tpl
---------
{* Absolute path, or relative to $ trusted_dir *}
{* Absolute path or relative path of $ 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 the php template engine smarty. For more information, see PHP Chinese network (www.php1.cn )!

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.