Three Common built-in methods for smarty learning notes

Source: Internet
Author: User

PHP built-in method
{$ Var = ...}
This method is short for {assign} and is used to define a variable directly in the template.
Attribute:
Scope: parent \ Root \ global
Support for nocache

{Append}
Create an array variable to the template variable during template calling, or append an element to a known array variable. nocache labels are supported.
Attribute:
VaR variable name
Value variable value
Index: The subscript index of the inserted variable in the array. If this value is not specified, it is added at the end of the array by default.
Scope access permission
Support for nocache labels

{Assign}
Same role as {$ Var = ...}
Attribute
Variable name defined by VAR
Variable value defined by value
Scope access permission
Support for nocache labels

{Call}
Call the method defined using the {function} tag in the template. This method must contain the name attribute. You can also pass the variable to the called method as an attribute.
Attribute:
Template Method to be called by name
Assign stores the values returned after calling the template method in the variables defined by assign, rather than directly outputting the returned values.
[Var...] Passing variables to the template method for use by the method
Support for nocache labels

{Capture}
Used to collect content that appears between {capture name = 'foo'} and {/capture} blocks, and save the content to the capture block tag named Foo, if the name attribute is not specified, default is used. You can use $ smarty. Capture. Foo to access the saved content. This label cannot be nested.
Attribute:
Namecapture block label name
If this attribute is specified for assign, the captured content is assigned to the specified variable.
Append: append the captured content to the specified array.
Support for nocache labels

{Config_load}
Used to obtain variable information in the configuration file
Attribute:
File: the configuration file name to be read.
Section reads the specified part in the configuration file.
Scope permission allocation

{Debug}
The debug control page is displayed. This setting will not be affected by the debug setting in the PHP script.

{Extends}
This label is used in the subtemplate to specify the parent template to be inherited.
This label must appear in the first line of the subtemplate
If the sub-template uses the {extends} tag to inherit the parent template, it can only contain the {block} tag (content), and the content of any other template will be ignored;
Use this syntax to inherit files outside the $ template_dir directory for template resources.
Attribute:
Template Name to be inherited by file

{}
The {for} {forelse} tag is used for a simple loop. It supports the following two formats:
The step {for $ Var = $ start to $ end} is 1.
{For $ Var = $ start to $ end step $ step} specifies the step size.
If the loop is empty, run {forelse}
Attribute:
Max limit the maximum number of cycles
Support for nocache labels

{Foreach}, {foreachelse}
It is used to perform a simple loop on an array of data in the following formats:
{Foreach $ array as $ Val}
{Foreach $ array as $ key => $ Val}
Execute the {foreachelse} statement when the loop content is empty.
{Foreach} has the following attributes: @ index, @ iteration, @ first, @ last, @ show, @ total.
{Break} and {continue} are supported}

{Function}
This interface is used to create a template function within a template. The Calling method is similar to that of a plug-in.
Note: The template function is global. To call the template function of an external template, you must use the {call} tag, you can use {functionname} to call the template function of your template.

{If}, {elseif}, {else}
Similar to PhP, the end flag must be {/if. All PHP conditional functions and methods can be used (is_array ()).

{Include}
It is used to introduce other templates to the current template. Any valid variables in the current template are also available in the included template.
You must specify the file attribute, which specifies the template resource location.
Set the assign attribute option to specify the template variable to which the output of {include} is stored without displaying the content, similar to {assign }.
Variables can be passed as attribute parameters to the included template. Any variables explicitly passed to the included template are valid only in the scope of the included file. If the passed attribute variable has a variable with the same name in the current template, the passed attribute variable overwrites the current template variable.
All variables of the main template can be used within the contained template, but only the local scope is available for variables changed or created in the contained template, after the {include} statement is used, these variables are invisible within the main template. There are two ways to change this default behavior (variable scope ): use the scope attribute in the {include} statement to be valid for all the values assigned to the template to be included, or use the scope attribute in the {assign} statement to change the scope of individual variables. The latter feature is useful when returning values from the included template to the master template.
Use the template resource syntax for templates that do not contain the $ template_dir folder.
Supports nocache, caching, and cache_lifetime labels.
Inline label: sets this item and will be compiled to include files (useful)

{Include_php} {insert} is not in favor of use

{Ldelim}, {rdelim}
Outputs the left and right labels of smarty.

{Nocache}
It must be paired with {/nocache. Used to disable the caching of a part in the template. That is, the content that appears in the block is not cached.

{Section}, {sectionelse}
Loop the arrays of consecutive numeric subscript indexes. {Foreach} cannot be used to loop the value index array. We recommend that you use {foreach}

{Setfilter}
It must be paired with the {/setfilter} tag. All variables that appear in this block label are filtered.
Syntax: {setfilter filter1 | filter2 | filter3....}... {/setfilter}
This label can be nested. The embedded tag filter will overwrite the upper filter.

{While}
Similar to the while statement in PHP. All PHP conditional functions and methods can be used (is_array ()).
PHP Technology Exchange Group 170855791

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.