目录搜索
Smarty模板编译引擎I.开始第一章. 什么是Smarty?第二章. 安装要求基本安装扩展设置II.模板设计者篇第三章.基本语法注释函数属性第四章.变量从PHP分配的变量Associative arraysArray indexesObjects从配置文件读取的变量{$smarty}保留变量Request variables{$smarty.now}{$smarty.const}{$smarty.capture}{$smarty.config}{$smarty.section}{$smarty.template}第五章.变量调节器capitalizecount_characterscatcount_paragraphscount_sentencescount_wordsdate_formatdefaultescapeindentlowernl2brregex_replacereplacespacifystring_formatstripstrip_tagstruncateupperwordwrap第六章.组合修改器第七章.内建函数captureconfig_loadforeachincludeinclude_phpinsertifldelimliteralphpsectionindexindex_previndex_nextiterationfirstlastrownumloopshowtotalstrip第八章.自定义函数assigncountercycledebugevalfetchhtml_checkboxeshtml_imagehtml_optionshtml_radioshtml_select_datehtml_select_timehtml_tablemathmailtopopup_initpopuptextformat第九章.配置文件第十章.调试控制台III.模板程序员篇第十一章 常量SMARTY_DIR第十二章 变量$template_dir$compile_dir$config_dir$plugins_dir$debugging$debug_tpl$debugging_ctrl$global_assign$undefined$autoload_filters$compile_check$force_compile$caching$cache_dir$cache_lifetime$cache_handler_func$cache_modified_check$config_overwrite$config_booleanize$config_read_hidden$config_fix_newlines$default_template_handler_func$php_handling$security$secure_dir$security_settings$trusted_dir$left_delimiter$right_delimiter$compiler_class$request_vars_order$compile_id$use_sub_dirs$default_modifiers$default_resource_type第十三章.方法appendappend_by_refassignassign_by_refclear_all_assignclear_all_cacheclear_assignclear_cacheclear_compiled_tplclear_configconfig_loaddisplayfetchget_config_varsget_registered_objectget_template_varsis_cachedload_filterregister_blockregister_compiler_functionregister_functionregister_modifierregister_objectregister_outputfilterregister_postfilterregister_prefilterregister_resourcetrigger_errortemplate_existsunregister_blockunregister_compiler_functionunregister_functionunregister_modifierunregister_objectunregister_outputfilterunregister_postfilterunregister_prefilterunregister_resource第十四章.缓存Setting Up CachingMultiple Caches Per PageCache GroupsControlling Cacheability of Plugins' Output第十五章.高级特点ObjectsPrefiltersPostfiltersOutput FiltersCache Handler FunctionResourcesTemplates from $template_dirTemplates from any directoryTemplates from other sourcesDefault template handler function第十六章.以插件扩展SmartyHow Plugins WorkNaming ConventionsWriting PluginsTemplate FunctionsModifiersBlock FunctionsCompiler FunctionsPrefilters/PostfiltersOutput FiltersResourcesInsertsⅣ.高级特点第十七章.疑难解答Smarty/PHP errors第18章.使用技巧和经验Blank Variable HandlingDefault Variable HandlingPassing variable title to header templateDatesWAP/WMLComponentized TemplatesObfuscating E-mail Addresses第十九章. 相关资源第二十章. 漏洞
文字

section,sectionelse

Table of Contents目录
index
index_prev
index_next
iteration
first
last
rownum
loop
show
total

Attribute Name Type Required Default Description
name string Yes n/a The name of the section
loop [$variable_name] Yes n/a The name of the variable to determine # of loop iterations
start integer No 0 The index position that the section will begin looping. If the value is negative, the start position is calculated from the end of the array. For example, if there are seven values in the loop array and start is -2, the start index is 5. Invalid values (values outside of the length of the loop array) are automatically truncated to the closest valid value.
step integer No 1 The step value that will be used to traverse the loop array. For example, step=2 will loop on index 0,2,4, etc. If step is negative, it will step through the array backwards.
max integer No 1 Sets the maximum number of times the section will loop.
show boolean No true determines whether or not to show this section

属性 类型 是否必须 缺省值 描述
name string Yes n/a 该循环的名称
loop [$variable_name] Yes n/a 决定循环次数的变量名称
start integer No 0 循环执行的初始位置. 如果该值为负数,开始位置从数组的尾部算起. 例如:如果数组中有7个元素,指定start为-2,那么指向当前数组的索引为5. 非法值(超过了循环数组的下限)将被自动调整为最接近的合法值.
step integer No 1 该值决定循环的步长. 例如指定step=2将只遍历下标为0、2、4等的元素. 如果step为负值,那么遍历数组的时候从后向前遍历.
max integer No 1 设定循环最大执行次数.
show boolean No true 决定是否显示该循环.

Template sections are used for looping over arrays of data. All section tags must be paired with /section tags. Required parameters are name and loop . The name of the section can be anything you like, made up of letters, numbers and underscores. Sections can be nested, and the nested section names must be unique from each other. The loop variable (usually an array of values) determines the number of times the section will loop. When printing a variable within a section, the section name must be given next to variable name within brackets []. sectionelse is executed when there are no values in the loop variable.

模板的 section 用于遍历数组中的数据. section 标签必须成对出现. 必须设置 nameloop 属性. 名称可以是包含字母、数字和下划线的任意组合. 可以嵌套但必须保证嵌套的 name 唯一. 变量 loop (通常是数组)决定循环执行的次数. 当需要在 section 循环内输出变量时,必须在变量后加上中括号包含着的 name 变量. sectionelse 当 loop 变量无值时被执行.

Example 7-15. section
例 7-15. section 函数演示

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

OUTPUT:

id: 1000<br>
id: 1001<br>
id: 1002<br>

Example 7-16. section loop variable
例 7-16.loop 变量演示

{* 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]}<br>
	name: {$name[customer]}<br>
	address: {$address[customer]}<br>
	<p>
{/section}


OUTPUT:

id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
<p>

Example 7-17. section names
例 7-17. 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]}<br>
	name: {$name[mydata]}<br>
	address: {$address[mydata]}<br>
	<p>
{/section}

Example 7-18. nested sections
例 7-18. 嵌套 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]}<br>
	name: {$name[customer]}<br>
	address: {$address[customer]}<br>
	{section name=contact loop=$contact_type[customer]}
		{$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br>
	{/section}
	<p>
{/section}


OUTPUT:

id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: john@mydomain.com<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@mydomain.com<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@mydomain.com<br>
<p>

Example 7-19. sections and associative arrays
例 7-19. section 遍历多维数组演示

{* This is an example of printing an associative array
 of data within a section *}
{section name=customer loop=$contacts}
	name: {$contacts[customer].name}<br>
	home: {$contacts[customer].home}<br>
	cell: {$contacts[customer].cell}<br>
	e-mail: {$contacts[customer].email}<p>
{/section}


OUTPUT:

name: John Smith<br>
home: 555-555-5555<br>
cell: 555-555-5555<br>
e-mail: john@mydomain.com<p>
name: Jack Jones<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@mydomain.com<p>
name: Jane Munson<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@mydomain.com<p>

Example 7-20. sectionelse
例 7-20. sectionelse 演示

{* sectionelse will execute if there are no $custid values *}
{section name=customer loop=$custid}
	id: {$custid[customer]}<br>
{sectionelse}
	there are no values in $custid.
{/section}

Sections also have their own variables that handle section properties. These are indicated like so: {$smarty.section.sectionname.varname}

Section 循环也有可供调用的变量名. 通过如下方式调用{$smarty.section.sectionname.varname}.

NOTE: As of Smarty 1.5.0, the syntax for section property variables has been changed from {%sectionname.varname%} to {$smarty.section.sectionname.varname}. The old syntax is still supported, but you will only see reference to the new syntax in the manual examples.

注意:Smarty 1.5.0 版中,section 名称属性变量的格式由{%sectionname.varname%}变成 {$smarty.section.sectionname.varname},老版本的格式依然支持,但在手册的例子中只提供新的格式.

上一篇:下一篇: