PHP template engine Smarty One of the built-in functions

Source: Internet
Author: User
Tags key string php template
Smarty comes with some built-in functions.
The built-in functions are part of the template language.
Users cannot create custom functions with the same name and built-in functions, nor can they modify the built-in functions.
(Insert, If,elseif,else, Ldelim,rdelim, Literal, PHP, Section,sectionelse, strip, etc. built-in functions refer to the PHP template engine smarty built-in functions of the second)
#capture函数
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 variable name where to assign the captured output to

Whether the property type must have a default value description
Name string no default data collection area names
Assign string No N/A data acquisition area where to assign the variable name[be deciphered]

The function of the capture function is to capture data from the template output and store it in a variable instead of outputting it to the page.
Any data between {capture Name= "foo"} and {/capture} will be stored in the variable $foo, which is specified by the Name property.
Access the variable by $smarty. Capture.foo in the template.
If you do not specify a Name property, the function defaults to use "default" as the parameter.
{capture} must appear in pairs, that is, {/capture} as the end, the function cannot be nested.
Cases:
{* This example outputs a row of tables containing data after capturing the content, and does not output anything if it is not captured *}
{Capture Name=banner}
{include file= "GET_BANNER.TPL"}
{/capture}
{if $smarty. Capture.banner NE "}
<tr>
<td>
{$smarty. Capture.banner}
</td>
</tr>
{/if}


Config_load
To load a variable from a configuration file
Whether the property type must have a default value description
File string Yes N/a The name of the configuration file to include
Section string No N/A name of the part to be loaded in the configuration file
Scope string No local load data must be in local, parent, or global scope. Local indicates that the variable is scoped to the current template. The parent indicates that the variable is scoped to the current template and to the parent template of the current template (the template that invokes the current template). Global indicates that the variable is scoped to all templates.
Global Boolean No no indicates whether the loaded variable is globally visible, equivalent to Scope=parent. Note: When the scope property is specified, the property can be set, but the template ignores the property value and is subject to the scope property.


Cases:
{config_load file= "colors.conf"}
<title>{#pageTitle #}</title>
<body bgcolor= "{#bodyBgColor #}" >
<table border= "{#tableBorderSize #}" bgcolor= "{#tableBgColor #}" >
<tr bgcolor= "{#rowBgColor #}" >
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>

The configuration file may contain multiple parts, and you can use the attached Properties section to specify from which part the variable is obtained.
Note: The section in the configuration file and the template built-in function sections are only named the same, irrelevant.
Cases:
{config_load file= "colors.conf" section= "Customer"}
<title>{#pageTitle #}</title>
<body bgcolor= "{#bodyBgColor #}" >
<table border= "{#tableBorderSize #}" bgcolor= "{#tableBgColor #}" >
<tr bgcolor= "{#rowBgColor #}" >
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>

Foreach,foreachelse

foreach is another scenario for processing loops other than section (choose a different scenario depending on your needs).
foreach is used to work with simple arrays (the same type of elements in an array), which is much simpler to format than the section, with the drawback that only simple arrays can be processed.
foreach must be paired with/foreach and must specify the From and item properties.
The Name property can be arbitrarily specified (a combination of letters, numbers, and underscores).
foreach can be nested, but you must ensure that the foreach names in the nested are unique.
The From property (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 have a default value description
From string Yes N/A name of the array to be looped
Item string Yes n/A variable name of the current processing element
Key string No N/a The name of the current processing element

Name string No N/A names the loop that is used to access the

Example 1:
{* This example outputs the value of all elements in an array $custid *}
{foreach from= $custid item=curr_id}
ID: {$curr _id}<br>
{/ foreach}
Output:
Id:1000<br>
Id:1001<br>
Id:1002<br>
Example 2:
$smarty->assign ( "Contacts", Array ("Phone" = "1", "fax" = "2", "Cell" = "3"),
Array ("Phone" = "555-4444", "Fax" =& Gt "555-3333", "cell" = "760-1234")));
*} The
{* key is the subscript for the array, see explanation of the array *}
{foreach Name=outer item=contact from= $contacts}
{foreach Key=key item=item from=$ Contact}
{$key}: {$item}<br>
{/foreach}
{/foreach}
Output:
Phone:1<br>
Fax:2<br
Cell:3<br>
Phone:555-4444<br>
Fax:555-3333<br>
Cell:760-1234<br>

The Foreach loop has its own variable name, which can be accessed by using the variable name. Use the method {$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 property, which indicates the location of the template resource.
If the Assign property is set, the variable name of the property is used to hold the output of the template to be included so that the output of the template to be included is not displayed directly.
Whether the property type must have a default value description
File string Yes N/a template file name to include
Assign string No N/A The property specifies a variable to hold the output of the template to be included
[var ...] [Var type] No N/A local parameter passed to the template to be included, only valid in the template to be included

Example 1 include function demo
{include file= "HEADER.TPL"}
{* Body of template goes here *}
{include file= "FOOTER.TPL"}

Example 2 demonstration of an include function 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 demonstration of an include function using an external template resource
{* 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"}

The

include_php
inluce_php function is used to include PHP scripts in the template. If Safe mode is set, the included script must be located under the $trusted _dir path. The include_php function must set the file property, This property indicates the path to the containing PHP file, either a relative path to the $trusted _dir or an absolute path. The
include_php is a good way to solve template widgets, which allows the PHP code to be detached from the template file. For example: Suppose you have a template that dynamically pulls data from a database to display site navigation, you can get the data content of PHP The logical part is separated and saved in a separate folder and contains the PHP script at the beginning of the template. Then you can include the template anywhere without worrying about whether the database information has been removed by the program.
Even if you call PHP files multiple times in a template, they are included only once by default. You can set the once property to indicate that each call is re-included. If the once property is set to False, each call to the file will be re-included.
If the Assign property is set, the variable name of the property is used to hold the output of the PHP to be included so that the output from the PHP file will not be displayed directly. The
Smarty object can be accessed by $this in the PHP file to be included.
whether the property type must have a default value description
File string Yes N/a The name of the PHP file to include
once Boolean No true if the included PHP file is still included (similar to PHP include_onc e function)
Assign string No N/A The property specifies a variable to save the output of the PHP file to be included

Cases
<?php
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 (' 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}
<a href= "http://www.php1.cn/" > {/foreach}

These are the contents of one of the built-in functions of the PHP template engine smarty, please pay attention to topic.alibabacloud.com (www.php.cn) for more information!

  • Related Article

    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.