Smarty Template engine built-in function usage, smarty template engine functions
In this paper, we describe the use of smarty built-in functions. Share to everyone for your reference. Specific as follows:
In-build (built-in), in the Smarty template, provides a lot of built-in function libraries, specific use can refer to Smarty Chinese manual CHM version.
1.foreach function
The operation array is as follows:
Indexed array $res =array (' Shanghai ', ' Beijing ', ' Shenzhen '), $smarty->assign ("arr", $res);//associative array $res 2=array (' city1 ' = ' Beijing ', ' city2 ' = > ' guangzhou ', ' city3 ' = ' Hunan ', $smarty->assign ("arr2", $res 2);//Index two-dimensional array $res 3 = Array ( ' Xiao ', ' Changshan ', ' Wu Bei '), Array (' Shan Shan ', ' keep your '); $smarty->assign ("Arr3", $res 3);//Associative two-dimensional array $res 4 = array (' id ' = ' = ' 001 ', ' name ' = ' = ') Zhang San ', ' email ' = ' zhangsan@1163.com '), array (' url ' = ' http://www.baidu.com ', ' age ' = ') '); $smarty Assign ("Arr4", $res 4);//Associative two-dimensional array 2 $res 5=array (' emp1 ' =>array (' id ' = ' 001 ', ' name ' = = ' Zhang San ', ' email ' = = ') Zhangsan@1163.com '), ' emp2 ' =>array (' url ' = ' http://www.baidu.com ', ' age ' = ' 28 ')); $smarty->assign ("Arr5", $res 5);
To iterate over an array:
Where from, item, key is a fixed notation, key can be added according to demand
One-dimensional arrays
Indexed arrays:
<{foreach from= $arr item=temp}> <{$temp}>
Associative arrays:
<{foreach from= $arr 2 item=temp key=k}> <{$k}>=<{$temp}>
Note: From, item, key is fixed
Two-dimensional arrays
Two-dimensional indexed arrays:
<{foreach from= $arr 3 item=temp key=k}> <{* Here temp is an array *}> <{foreach from= $temp item=val}> <{$val}>
Two-dimensional associative array format 1:
<{foreach from= $arr 4 item=temp}> <{* The outer key is not required, so do not add key*}> <{foreach from= $temp item=val key=k} > <{* inner layer key needs, add key*}> <{$k}>=<{$val}>
Two-dimensional associative array format 2:
<{foreach from= $arr 5 item=temp key=k}> <{$k}>: <{foreach from= $temp item=val key=k2}> <{$k 2}>=<{$val}> <{/foreach}>
<{/foreach}>
2.if...else ...
<{if $age >10}> age is greater than 10, age: <{$age}> <{else}> age less than 10, age: <{$age}> <{/ If}>
3.if...elseif...elseif...else ...
The known data sources are as follows:
$res 4 = array (' id ' = ' = ' 001 ', ' age ' = ' 4 '), array (' id ' = ' = ' 002 ', ' age ' = ' + '), array (' ID ') = ' 003 ', ' age ' = ' + '), array (' id ' = ' 004 ', ' age ' = ' 80 '));
The references in the template are as follows:
<{foreach from= $arr 4 item=temp}> <{if $temp. Age < 5}> <{$temp. Id}>, you are a child <{ ElseIf $temp. Age >=5 and $temp. Age <= 18}> <{$temp. Id}> you are young <{elseif $temp. Age > $t Emp.age <= 50}> <{$temp id}> you are an adult <{else}> <{$temp. id}> <{/if}><{/foreach}
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/976026.html www.bkjia.com true http://www.bkjia.com/PHPjc/976026.html techarticle smarty Template engine built-in function usage, smarty template engine functions This article describes the use of smarty built-in functions. Share to everyone for your reference. Specific as follows: In-buil ...