Php-smarty Template Tutorial (2)

Source: Internet
Author: User
The foreachelese function indicates the code executed when the array does not have any data.

Part 1 (Portal): Introduction and entry to the smarty Template

------------------------ New content ------------ Smarty template design Article 4. built-in functions 1. capture syntax: {capture name = 'var'} name indicates to save the content to the specified variable content {/capture} capture the content between functions, rather than the output format: {$ smarty. capture. var} 2. config_load {config_load file = "file" section = "section"} load the configuration File file: Name of the configuration file to be loaded Section: in the configuration section to be loaded, there are two reference methods for variables in the configuration file: {# var #}{ $ smarty. config. var} 3. foreach, foreachelse {foreach from = array key = key name = name item = content} {foreachelse} {/foreach} forea The ch function is used to traverse the array foreachelese function. it indicates the code executed when the array does not have any data. From: the Key of the array to be traversed: the subscript Item of the retrieved array: Name of the element value of the retrieved array: name l foreach's built-in variable $ smarty for the current foreach. foreach. name. index: The index is sorted from 0 to $ smarty. foreach. name. iteration: the number of times from 1 to $ smarty. foreach. name. first: true $ smarty for the first execution. foreach. name. last: true $ smarty for the last execution. foreach. name. total: total number of cycles l preset variable of foreach 3.0 {foreach $ myarray as $ key =>$ value }{$ value @ iteration} the number of current cycles {$ value @ inde X} loop index {$ value @ first}: true in the first loop {$ value @ last}: true in the last loop {$ value @ total} total number of cycles {/foreach} 4, include {include file = "file" assign = "var" [var...]} include another template File: The template File to be included Assign: save the result of the File to be included to the specified variable. this time, [var…] is not output.: Variables that can pass parameter = value to the template in php can be used in the subtemplate.
5. include_php {include_php file = "file" assign = "var" once = "true | false"} contains a php File file: The php file to be included Assign: save the output result to the specified variable Once: whether to include only one example: now I want to display the latest system time in the template before smarty3.0, you can directly use the include_php built-in function, however, due to some security issues, after smarty3.0, this function was retained to SmartyBC. class. php file
6 insert {insert name = "func" assign = "var" [var...]} using insert, you can call a php user-defined function insert_func (){......} name: name of the function to be called. this function must be prefixed with insert _ in php. assign: save the output result [var…]. : Passing parameters to the function to be called
 
Include: content can be cached Insert: content will never be cached 7. if elseif else syntax: {if} {elseif} {else} {/if}
8. ldelim, rdeliml ldelim {ldelim }:{ l rdelim {rdelim }:} for example, on the page, you want to output {$ a} as-is {ldelim} $ a {rdelim} 9, literal {literal }...... The content between the {/literal} Literal tags will not be parsed by smarty.
10. php {php }...... {/Php} can add the php code in this tag to smartyBC in smarty3.0. class. if you want to use this file in php, you need to reference the file which is now put in smartyBc. class. php built-in functions may not be supported in the future.
11. section, sectionelse {section name = name loop = loop array (times) start = start (0) step = step (1) max = maximum number of cycles}
When the array specified by {sectionelse} loop does not have a value, {/sectionelse} {/section} loops through the specified array. the number of array elements determines the number of loops Name: Name Loop: loop array (the number of elements in this array determines the final number) the above two attributes are required. Start: Start Index Step: Step Max: maximum number of loops

L section's built-in variable {$ smarty. section. name. index}: index {$ smarty. section. name. index_prev}: the last index {$ smarty. section. name. index_next}: the next index {$ smarty. section. name. iteration}: the number of {$ smarty. section. name. first | last}: 1st and last time is true {$ smarty. section. name. total}: total number of cycles 12. strip {strip }...... Remove left and right spaces from {/strip}

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.