Home  >  Article  >  Backend Development  >  The second built-in function of PHP template engine smarty

The second built-in function of PHP template engine smarty

黄舟
黄舟Original
2016-12-20 10:56:551337browse

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.
(#capture function, config_load, foreach , foreachelse, include, include_php and other built-in functions, please refer to one of the built-in functions in the php template engine smarty tutorial topic) Whether the attribute type must be used Default value description
name string Yes n/a The name of the inserted function
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 It needs to be included before inserting the function PHP script name

[var ...] [var type] No n/a Local parameters passed to the function to be inserted

Insert function is similar to the inluce function, the difference is that the content contained in insert will not be cached. This function will be re-executed every time the template is called. For example, if you use a template with an advertising banner position at the top of the page, the advertising banner can contain any mixed information such as HTML, images, FLASH, etc. Therefore, a static one cannot be used here link, and we do not want the advertising banner to be cached. This requires specifying the #banner_location_id# and #site_id# values ​​​​(taken from the configuration file) in the insert function. At the same time, a function is needed to obtain the content information of the advertising banner.

insert function demonstration
{* example of fetching a banner *} {insert name="getBanner" lid=#banner_location_id# sid=#site_id#}

In this example, we use getBanner as the name attribute and pass # banner_location_id# and #site_id# two parameters. Next, Smarty searches for a function named insert_getBanner() in your PHP program. The values ​​​​of #banner_location_id# and #site_id# are combined into an array and passed as the first parameter of the function. to this function. To avoid function naming confusion, all insert functions must begin with insert_. Your insert_getBanner() function executes the arguments passed and returns the results of the execution. These results are displayed in the template at the point where the function is called. In this example, Smarty calls the function similar to insert_getBanner(array("lid"=>"12345","sid"=>67890")); and displays the returned result at the calling location.

If set assign attribute, 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 the template variable is also invalid when cached.

If If the script attribute is specified, the php script specified by script will be included (only once) before calling the function and executing it. This is to prevent the called function from not existing. Calling the php script containing the function first will avoid this situation.

Smart object is passed as the second parameter of the function. In the function to be included, you can access and modify the smarty object information through $this.

Technical point: Make part of the template not cached. If caching is turned on, the insert function does not will be cached, and they will be loaded dynamically every time the page is called, even in cached pages. This feature can be widely used in advertising banners, voting, real-time weather forecasts, 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 adds several features to suit 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, they must be separated from variables or constants by spaces.

Statement demonstration {if $name eq "Fred"} Welcome Sir.{ elseif $name eq "Wilma"} Welcome Ma'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 calls *}{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, which are braces "{" and "}". The template engine always tries to interpret the content inside the braces, so if you need to output braces, use this method.
Use ldelim, rdelim to demonstrate {* this will print literal delimiters out of the template *}{ldelim}funcname{rdelim} is how functions look in Smarty! Output results: {funcname} is how functions look in Smarty!


literal
The data in the Literal tag area will be treated as text, and the template will ignore all character information inside it. This feature is used to display javascript scripts that may contain character information such as braces. When this information is in {literal} {/literal} tags, the template engine will not analyze them and display them directly.

literal tag demonstration
{literal}
The
{/literal}

php

php tag allows php scripts to be embedded directly in the template. Whether these statements are processed depends on the setting of $php_handling. This statement usually does not need to be used, of course if you are very familiar with this feature or think it is necessary If you want, you can also use .

php tag demonstration
{php} // including a php script directly // from the template. include("/path/to/display_weather.php"); {/php}


section,sectionelse
The section of the template is used to traverse the data in the array. Section tags must appear in pairs. The name and loop attributes must be set. The name can be any combination of letters, numbers, and underscores. Nesting can be ensured but must be guaranteed The name is unique. The variable loop (usually an array) determines the number of loop executions. When a variable needs to be output within a section loop, the name variable enclosed in square brackets must be added after the variable. sectionelse is executed when the loop variable has no value. .

Whether the attribute type must have a default value description
name string Yes n/a The name of the loop
loop [$variable_name] Yes n/a The name of the variable that determines the number of loops
start integer No 0 The initial position of the loop execution. If The value is a negative number, and the starting position is calculated from the end of the array. For example: if there are 7 elements in the array and specify start as -2, then the index pointing to the current array is 5. Illegal values ​​(exceeding the lower limit of the loop array) will Is automatically adjusted to the nearest legal value.
step integer No 1 This value determines the step size of the loop. For example, specifying step=2 will only traverse elements with subscripts 0, 2, 4, etc. If step is a negative value, then When traversing the array, traverse from back to front.
max integer No 1 Set the maximum number of executions of the loop.
show boolean No true Determine whether to display the loop.


section Function demonstration
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
id: {$custid[customer]}

{/section}

Output result:

id: 1000

id: 1001

id: 1002

loop demonstration
Output result:
{* the loop variable only determines the number of times to loop. you can access any variable from the template within the section. This example assumes that $custid, $name and $address are all arrays containing the same number of values ​​*}{section name=customer loop=$custid} id: {$custid[customer]}
name: {$name [customer]}
address: {$address[customer]}

{/section}Output result: id: 1000
name: John Smith
address: 253 N 45th< ;br>

id: 1001
name: Jack Jones
address: 417 Mulberry ln

id: 1002
name: Jane Munson
address: 5605 apple st< ;br>

section 名称演示 {* the name of the section can be anything you like,
 and it is used to reference the data within the section *}
{section name=mydata loop=$custid}
 id: {$custid[mydata]}

 name: {$name[mydata]}

 address: {$address[mydata]}

 


{/section} 
嵌套 section 演示 {* sections can be nested as deep as you like. With nested sections, you can access complex data structures, such as multi-dimensional arrays. In this example, $contact_type[customer] is an array of contact types for the current customer. *}{section name=customer loop=$custid} id: {$custid[customer]}
name: {$name[customer]}
address: {$address[customer]}
{section name=contact loop=$contact_type[customer]}  {$contact_type[customer][contact]}: {$contact_info[customer][contact]}
{/section}

{/section}输出结果:id: 1000
name: John Smith
address: 253 N 45th
home phone: 555-555-5555
cell phone: 555-555-5555
e-mail: john@mydomain.com

id: 1001
name: Jack Jones
address: 417 Mulberry ln
home phone: 555-555-5555
cell phone: 555-555-5555
e-mail: jack@mydomain.com

id: 1002
name: Jane Munson
address: 5605 apple st
home phone: 555-555-5555
cell phone: 555-555-5555
e-mail: jane@mydomain.com

 
section 遍历多维数组演示 {* This is an example of printing an associative array
 of data within a section *}
{section name=customer loop=$contacts}
 name: {$contacts[customer].name}

 home: {$contacts[customer].home}

 cell: {$contacts[customer].cell}

 e-mail: {$contacts[customer].email}


{/section}


输出结果:

name: John Smith

home: 555-555-5555

cell: 555-555-5555

e-mail: john@mydomain.com


name: Jack Jones

home phone: 555-555-5555

cell phone: 555-555-5555

e-mail: jack@mydomain.com


name: Jane Munson

home phone: 555-555-5555

cell phone: 555-555-5555

e-mail: jane@mydomain.com

 
sectionelse 演示 {* sectionelse will execute if there are no $custid values *}
{section name=customer loop=$custid}
 id: {$custid[customer]}

{sectionelse}
 there are no values in $custid.
{/section}

Section 循环也有可供调用的变量名. 通过如下方式调用{$smarty.section.sectionname.varname}.
 
strip
Web 开发者多次遇到空格和回车影响HTML输出的情形(浏览器的"特性"),为了得到特定的结果,因此你不得不在模板里运行所有的标签. 通常在难以理解或难以处理的模板中遇到此问题.

Smarty 在显示前将除区任何位于 {strip}{/strip} 标记中数据的首尾空格和回车. 这样可以保证模板容易理解且不用担心多余的空格导致问题.
strip 标签演示 {* the following will be all run into one line upon output *}
{strip}


 
  
 

       This is a test
   

  

{/strip}


输出结果:

{strip}

 
  
 

       This is a test
   

  

{/strip}


输出结果:

Please note that in the above example, all lines start and end with HTML tags. All lines are organized and run together. If there is text at the beginning and end of the line, they will also be organized together, and you may get something you don't want. Result.

The above is the content of the second built-in function of PHP template engine smarty. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn