Usage of building functions within the smarty template engine, and functions of the smarty template engine
This article describes how to use the smarty built-in functions. Share it with you for your reference. The details are as follows:
In-build (built-in) provides many built-in function libraries in the smarty template. For more information, see the smarty Chinese manual chm version.
1. foreach Function
The operation array is as follows:
// Index array $ res = array ('shanghai', 'beijing', 'shenzhen'); $ smarty-> assign ("arr", $ res ); // join array $ res2 = array ('city1 '=> 'beijing', 'city2' => 'guangzhou ', 'city3' => 'hunan '); $ smarty-> assign ("arr2", $ res2); // index a two-dimensional array $ res3 = array ('xiao Xiao ', 'changshan ', 'wu bei'), array ('shanshan', 'changming'); $ smarty-> assign ("arr3", $ res3 ); // associate a two-dimensional array $ res4 = array ('id' => '001', 'name' => 'zhang san ', 'email '=> 'zhangsan @ mongo3.com'), array ('url' => 'HTTP: // www.baidu.com ', 'age' => '28 ')); $ smarty-> assign ("arr4", $ res4 ); // associate two-dimensional array 2 $ res5 = array ('emp1' => array ('id' => '001', 'name' => 'zhangsan ', 'email '=> 'zhangsan @ mongo3.com'), 'emp2' => array ('url' => 'HTTP: // www.baidu.com ', 'age' => '28'); $ smarty-> assign ("arr5", $ res5 );
Traverse the array:
Here, from, item, and key are fixed statements, and keys can be added as needed.
One-dimensional array
Index Array:
<Br/> <{foreach from = $ arr item = temp}> <{$ temp}> <t/> <{/foreach}> <br/> join an array: <br/> <{foreach from = $ arr2 item = temp key = k}> <{$ k} >=< {$ temp}> <t/> <{/foreach }>< br/>
Note: from, item, and key are fixed.
Two-dimensional array
<Br/> two-dimensional Index Array: <br/> <{foreach from = $ arr3 item = temp key = k}> <{* Here temp is an array *}> <{foreach from = $ temp item = val }><{$ val }><{/ foreach }> <br/> two-dimensional joined array format 1: <br/> <{foreach from = $ arr4 item = temp}> <{* The outer key is not required, so do not add key * }><{ foreach from = $ temp item = val key = k }><{ * the key in the inner layer needs to be, add key * }><{$ k }>=<{$ val }><{/ foreach }>< br/> two-dimensional joined array Format 2: <br/> <{foreach from = $ arr5 item = temp key = k}> <{$ k}>: <{foreach from = $ temp item = val key = k2 }><{$ k2 }>=< {$ val }><{/ foreach }>< br/> <{ /foreach}>
2. if... else...
<{If $ age> 10}> age greater than 10, age: <{$ age }>< {else}> age less than 10, age: <{$ age }>< {/if}>
3. if... elseif... else...
Known data sources are as follows:
$res4 = array( array('id'=>'001','age'=>'4'), array('id'=>'002','age'=>'16'), array('id'=>'003','age'=>'20'), array('id'=>'004','age'=>'80') );
The template references the following:
<{Foreach from = $ arr4 item = temp}> <{if $ temp. age <5 }>< {$ temp. id}>, you are a child <{elseif $ temp. age> = 5 and $ temp. age <= 18 }>< {$ temp. id}>, you are a young man <{elseif $ temp. age> 18 and $ temp. age <= 50 }>< {$ temp. id }>, you are an adult <{else }>< {$ temp. id }>, age <span style = "font-family: Consolas;"> relatively large </span> <{/if} ><{/ foreach}
I hope this article will help you with php programming.