Smarty template engine built-in function usage_PHP tutorial

WBOY
Release: 2016-07-13 09:59:09
Original
974 people have browsed it

Usage of built-in functions of smarty template engine

This article mainly introduces the usage of built-in functions of smarty template engine. It analyzes the foreach function and if... The usage of built-in functions such as else..., if...elseif...elseif...else... and other built-in functions have certain reference value. Friends in need can refer to it

The example in this article describes how to use smarty’s built-in functions. Share it with everyone for your reference. The details are as follows:

In-build (built-in), in the smarty template, many built-in function libraries are provided. For specific usage, please refer to the chm version of the smarty Chinese manual.

1.foreach function

The operand array is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//索引数组

$res=array('上海','北京','深圳');

$smarty->assign("arr",$res);

//关联数组

$res2=array('city1'=>'北京','city2'=>'广州','city3'=>'湖南');

$smarty->assign("arr2",$res2);

//索引二维数组

$res3 = array(

array('潇晓','常山','吴蓓'),array('珊珊','常明')

);

$smarty->assign("arr3",$res3);

//关联二维数组

$res4 = array(

array('id'=>'001','name'=>'张三','email'=>'zhangsan@1163.com'),

array('url'=>'http://www.baidu.com','age'=>'28')

);

$smarty->assign("arr4",$res4);

//关联二维数组2

$res5=array(

'emp1'=>array('id'=>'001','name'=>'张三','email'=>'zhangsan@1163.com'),

'emp2'=>array('url'=>'http://www.baidu.com','age'=>'28')

);

$smarty->assign("arr5",$res5);

1 2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9


<{foreach from=$arr item=temp}>

<{$temp}>

<{/foreach}>


关联数组:

<{foreach from=$arr2 item=temp key=k}>

<{$k}>=<{$temp}>

<{/foreach}>


10 11 12 13 14 15 16 17 18 19 20 21 22 23
//Index array $res=array('Shanghai','Beijing','Shenzhen'); $smarty->assign("arr",$res); //Associative array $res2=array('city1'=>'Beijing','city2'=>'Guangzhou','city3'=>'Hunan'); $smarty->assign("arr2",$res2); //Index two-dimensional array $res3 = array( array('Xiaoxiao','Changshan','Wu Bei'), array('Shanshan','Changming') ); $smarty->assign("arr3",$res3); //Associated two-dimensional array $res4 = array( array('id'=>'001','name'=>'Zhang San','email'=>'zhangsan@1163.com'), array('url'=>'http://www.baidu.com','age'=>'28') ); $smarty->assign("arr4",$res4); //Associated two-dimensional array 2 $res5=array( 'emp1'=>array('id'=>'001','name'=>'Zhang San','email'=>'zhangsan@1163.com'), 'emp2'=>array('url'=>'http://www.baidu.com','age'=>'28') ); $smarty->assign("arr5",$res5);
Traverse the array: Among them, from, item, and key are fixed writing methods, and key can be added according to needs One-dimensional array Index array: ?
1 2 3 4 5 6 7 8 9
<{foreach from=$arr item=temp}> <{$temp}> <{/foreach}>
Associative array:
<{foreach from=$arr2 item=temp key=k}> <{$k}>=<{$temp}> <{/foreach}>

Note: from, item, key are fixed
Two-dimensional array

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23


二维索引数组:

<{foreach from=$arr3 item=temp key=k}>

<{*这里的temp是一个数组*}>

<{foreach from=$temp item=val}>

<{$val}>

<{/foreach}>

<{/foreach}>


二维关联数组格式1:

<{foreach from=$arr4 item=temp}>

<{*外层的键不需要,所以不添加key*}>

<{foreach from=$temp item=val key=k}>

<{*内层的键需要,添加key*}>

<{$k}>=<{$val}>

<{/foreach}>

<{/foreach}>


二维关联数组格式2:

<{foreach from=$arr5 item=temp key=k}>

<{$k}>:

<{foreach from=$temp item=val key=k2}>

<{$k2}>=<{$val }>

<{/foreach}>


<{/foreach}>

1 2

3

4

5

6

1

2

3

4

5

<{if $age>10 }>

年龄大于10,年龄为:<{$age}>

<{else}>

年龄小于10,年龄为:<{$age}>

<{/if}>

7 8

9

10

11

12

1

2

3

4

5

6

$res4 = array(

array('id'=>'001','age'=>'4'),

array('id'=>'002','age'=>'16'),

array('id'=>'003','age'=>'20'),

array('id'=>'004','age'=>'80')

);

13 14 15 16 17 18 19 20 21 22 23

Two-dimensional index array:
<{foreach from=$arr3 item=temp key=k}> <{*temp here is an array*}> <{foreach from=$temp item=val}> <{$val}> <{/foreach}> <{/foreach}>
Two-dimensional associative array format 1:
<{foreach from=$arr4 item=temp}> <{*The outer key is not needed, so no key is added*}> <{foreach from=$temp item=val key=k}> <{*The inner key is required, add key*}> <{$k}>=<{$val}> <{/foreach}> <{/foreach}>
Two-dimensional associative 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... ?
1 2 3 4 5 <{if $age>10 }> Age greater than 10, age: <{$age}> <{else}> If you are less than 10 years old, your age is: <{$age}> <{/if}>
3.if...elseif...elseif...else... The known data sources are as follows: ?
1 2 3 4 5 6 $res4 = array( array('id'=>'001','age'=>'4'), array('id'=>'002','age'=>'16'), array('id'=>'003','age'=>'20'), array('id'=>'004','age'=>'80') );

The template is quoted as follows:

?

1

2

3

4

5

6

7

8

9

10

11

<{foreach from=$arr4 item=temp }>

<{if $temp.age < 5}>

<{$temp.id}>,你是小孩

<{elseif $temp.age >=5 and $temp.age <= 18}>

<{$temp.id}>,你是年轻人

<{elseif $temp.age > 18 and $temp.age <= 50}>

<{$temp.id}>,你是成年人

<{else}>

<{$temp.id}>,年龄比较大了

<{/if}>

<{/foreach}

1 2

3

4 5

67 8 9 10 11
<{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}>, agerelatively older <{/if}> <{/foreach}
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/976541.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/976541.htmlTechArticleUsage of built-in functions of smarty template engine This article mainly introduces the usage of built-in functions of smarty template engine, examples Analyzed the foreach function, if...else..., if...elseif...else... in smarty
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template