This article describes how to create a function in the smarty template engine. The example analyzes the foreach function, if... else..., if... elseif... elseif... else... the usage of built-in functions has some reference value. if you need it, you can refer to the example in this article to describe how to use the built-in functions of smarty. 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:
<{Foreach from = $ arr item = temp}> <{$ temp}>
<{/Foreach}>
Join array:
<{Foreach from = $ arr2 item = temp key = k}> <{$ k} >=< {$ temp}>
<{/Foreach}>
Note: from, item, and key are fixed.
Two-dimensional array
Two-dimensional index array:
<{Foreach from = $ arr3 item = temp key = k}> <{* here temp is an array * }>< {foreach from = $ temp item = val}> <{$ val }><{/ foreach }>< {/foreach}>
Two-dimensional joined array format 1:
<{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}>
Two-dimensional joined array format 2:
<{Foreach from = $ arr5 item = temp key = k}> <{$ k}>: <{foreach from = $ temp item = val key = k2 }><{$ k2 }>=< {$ val }><{/ foreach}>
<{/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 }>, older <{/if}> <{/foreach}
I hope this article will help you with php programming.