How to Create functions in the smarty template engine _ PHP Tutorial

Source: Internet
Author: User
Create a function in the smarty template engine. This article describes how to create a function in the smarty template engine. The example analyzes the foreach function, if... else..., if... elseif... usage of creating functions in the else smarty template engine

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... and other built-in functions for reference.

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

// Index the array

$ Res = array ('Shanghai', 'Beijing', 'shenzhen ');

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

// Associate an array

$ Res2 = array ('city1 '=> 'Beijing', 'city2' => 'Guangzhou ', 'city3' => 'hunan ');

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

// Index two-dimensional array

$ Res3 = array (

Array ('Xiao Xiao ', 'changshan', 'Wu Bei'), array ('shanshan', 'changming ')

);

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

// Associate a two-dimensional array

$ Res4 = array (

Array ('id' => '001', 'name' => 'Zhang San', 'Email '=> 'zhangsan @ zhang3.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 @ zhang3.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:

?

1

2

3

4

5

6

7

8

9


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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23


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 no key is added *}>

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

<{* Key required for the inner layer, add key *}>

<{$ K }>=< {$ val}>

<{/Foreach}>

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

?

1

2

3

4

5

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

?

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 references the following:

?

1

2

3

4

5

6

7

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}> older

<{/If}>

<{/Foreach}

I hope this article will help you with php programming.

This article describes how to create a function in the smarty template engine. The example analyzes 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.