Smarty Template engine built-in function usage _php tutorial

Source: Internet
Author: User

Smarty Template engine built-in function usage


This article mainly introduces the Smarty template engine built-in function usage, the example analysis of smarty in the foreach function, if...else ..., if...elseif...elseif...else ... such as the use of built-in functions, with a certain reference value, the need for friends can refer to the following

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

Indexed array

$res =array (' Shanghai ', ' Beijing ', ' Shenzhen ');

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

Associative arrays

$res 2=array (' city1 ' = ' Beijing ', ' city2 ' and ' Guangzhou ', ' City3 ' and ' Hunan ');

$smarty->assign ("arr2", $res 2);

Indexed two-dimensional arrays

$res 3 = Array (

Array (' Xiao Hsiao ', ' Changshan ', ' Wu Bei '), Array (' Sunshine ', ' Keep your ')

);

$smarty->assign ("Arr3", $res 3);

Associative two-dimensional arrays

$res 4 = Array (

Array (' id ' = ' 001 ', ' name ' = ' Zhang San ', ' email ' = ' zhangsan@1163.com '),

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

);

$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:

?

1

2

3

4

5

6

7

8

9


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

<{$temp}>

<{/foreach}>


Associative arrays:

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

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

<{/foreach}>


Note: From, item, key is fixed
Two-dimensional arrays

?

1

2

3

4

5

6

7

8

9

13 /p>

+

/

/

/

/

/

+

+


Two-dimensional indexed arrays:

<{foreach from= $arr 3 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= $arr 4 item=temp}>

<{* the outer key is not required, so do not add key*}>

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

<{* the inner layer of the key needs, add key*}>

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

<{/foreach}>

<{/foreach}>


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 ...

?

1

2

3

4

5

<{if $age >10}>

Age is greater than 10, age: <{$age}>

<{else}>

Age is less than 10, age: <{$age}>

<{/if}>

3.if...elseif...elseif...else ...

The known data sources are as follows:

?

1

2

3

4

5

6

$res 4 = Array (

Array (' id ' = ' 001 ', ' age ' = ' 4 '),

Array (' id ' = ' 002 ', ' age ' = ' 16 '),

Array (' id ' = ' 003 ', ' age ' = ' 20 '),

Array (' id ' = ' 004 ', ' age ' = ' 80 ')

);

The references in the template are as follows:

?

1

2

3

4

5

6

7

8

9

10

11

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

<{if $temp. Age < 5}>

<{$temp. Id}> you're a child.

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

<{$temp. Id}> you're a young man.

<{elseif $temp. Age > $temp. Age <= 50}>

<{$temp. Id}> you're an adult.

<{else}>

<{$temp. Id}>

<{/if}>

<{/foreach}

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/976541.html www.bkjia.com true http://www.bkjia.com/PHPjc/976541.html techarticle smarty Template engine built-in function usage This article mainly introduces the Smarty template engine built-in function usage, the example analyzes the smarty in the foreach function, if...else ..., if...elseif...else ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.