The second built-in function of the php template engine smarty

Source: Internet
Author: User
Tags php template
You cannot create custom functions with the same name as built-in functions, or modify built-in functions.
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.
(# One of the built-in functions such as capture function, config_load, foreach, foreachelse, include, and include_php can be found in the smarty usage tutorial of the php template engine)
Insert
If, elseif, else
Ldelim, rdelim
Literal
Php
Section, sectionelse
Strip
 
Insert
Whether the property type must be the default value description
Name string Yes n/a insert function name
Assign string No n/a this attribute specifies a variable to save the output of the function to be inserted
Script string No n/a name of the php script that needs to be included before inserting a function
 
[Var...] [var type] No n/a is passed to the local parameter Insert function of the function to be inserted. the insert function is similar to the inluce function. The difference is that the content contained in Insert is not cached, this function is re-executed every time this template is called. for example, if you use a template with the ad bar position on the top of the page, the ad bar can contain any HTML, image, FLASH, and other mixed information. therefore, a static link cannot be used here, and we do not want the ad bar to be cached. in this case, you need to specify the values # banner_location_id # and # site_id # in the insert function (from the configuration file). at the same time, you need a function to retrieve the content of the advertisement bar.
 
Insert function demo
{* Example of fetching a banner *} {insert name = "getBanner" lid = # banner_location_id # sid = # site_id #} in this example, getBanner is used as the name attribute, the # banner_location_id # and # site_id # parameters are also passed. next, Smarty searches for a function named insert_getBanner () in your php program, # banner_location_id # and # site_id # are combined into an array and passed to the function as the first parameter of the function. to avoid function naming confusion, all insert functions must begin with insert. your insert_getBanner () function is executed based on the passed parameters and returns the execution result. these results are displayed in the template where the function is called. in this example, Smarty calls this function like insert_getBanner (array ("lid" => "12345", "sid" => 67890 ")); the returned results are displayed at the called location.
 
If the assign attribute is set, the variable name corresponding to this attribute is used to save the output of the function to be included, so that the output of the function to be included will not be displayed directly. note: The output information assigned to template variables is also invalid during cache.
 
If the script attribute is specified, the php script specified by the script is included (only once) before the function is called and executed. this is to prevent the called function from having exists. First, you can call the php script containing the function to avoid this situation.
 
The Smart object is passed as the second parameter of the function. you can access and modify the smarty object information through $ this in the function to be included.
Technical tips: Make part of the template not cached. if the cache is enabled, the insert function will not be cached, and each call to the page will be dynamically loaded, even in the cache page. this feature can be widely used in advertising, voting, real-time weather forecasting, search results, feedback information, and other areas.
 
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 fit the template engine. if must appear in pairs with/if. else and elseif clauses can be used. you can use the following conditional modifiers: 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, it must be opened with an empty lattice with variables or constants.
Statement demonstration {if $ name eq "Fred"} Welcome Sir. {elseif $ name eq "Wilma"} Welcome m'am. {else} Welcome, whatever you are. {/if} {* an example with "or" logic *} {if $ name eq "Fred" or $ name eq "Wilma "}... {/if} {* same as above *} {if $ name = "Fred" | $ name = "Wilma "}... {/if} {* the following syntax will NOT work, conditional qualifiers must be separated from surrounding elements by spaces *} {if $ name = "Fred" | $ name = "Wilma "}... {/if} {* parenthesis are allowed *} {if ($ amount <0 or $ amount> 1000) and $ volume >=# minVolAmt #}... {/if} {* you can also embed php function CILS *} {if count ($ var) gt 0 }... {/if} {* test if values are even or odd *} {if $ var is even }... {/if} {if $ var is odd }... {/if} {if $ var is not odd }... {/if} {* test if var is divisible by 4 *} {if $ var is div by 4 }... {/if} {* test if var is even, grouped by two. i. e ., 0 = even, 1 = even, 2 = odd, 3 = odd, 4 = even, 5 = even, etc. *} {if $ var is even by 2 }... {/if} {* 0 = even, 1 = even, 2 = even, 3 = odd, 4 = odd, 5 = odd, etc. *} {if $ var is even by 3 }... {/if} ldelim, rdelim
Ldelim and rdelim are used to output delimiters, that is, braces "{" and "}". The template engine always tries to explain the content in braces. Therefore, if you need to output braces, use this method.
Use ldelim and rdelim to demonstrate {* this will print literal delimiters out of the template *} {ldelim} funcname {rdelim} is how functions look in Smarty! Output result: {funcname} is how functions look in Smarty!
Literal
Data in the Literal label area is processed as text, and all characters in the template are ignored. this feature is used to display javascript scripts that may contain characters such as braces. when the information is in the {literal} {/literal} tag, the template engine directly displays the information without analyzing them.
 
Literal label demonstration
{Literal}
 

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.