Smarty control statement

Source: Internet
Author: User
The following three methods are common: if-elseif-else, foreach, and section, which are used to control the content output structure. Therefore, in today's php Tutorial: this tutorial is partly from the Internet, partly from the manual and provides a smarty Chinese hand

The following three methods are common: if-elseif-else, foreach, and section, which are used to control the content output structure. Therefore, in today's php Tutorial: this tutorial is partly from the Internet, partly from the manual, and provides the smarty Chinese manual download)

1. conditional selection structure if-elseif-else

<{If expression}>... <{else}>... <{/if}>

Conditional modifier: eq, ne, neq, gt, lt, lte, le, gte, ge, is even, is odd, is not even, is not odd, not, mod, div by, even by, odd by, = ,! =,>, <, <=,> =.

 

Explanation:

Equal: equal, not equal: not equal to, greater than: greater than, less than: less than, less than or equal: less than or equal to, great than or equal: greater than or equal to, is even: is an even number, is odd: is an odd number, is not even: not an even number, is not odd: not an odd number, not: not, mod: Remainder, div by: is... Division

 


{if $name eq "admin"} ......{elseif $name eq "Wilma"}}......{else}......{/if} 

2. use foreach for loop traversal

<{Foreach expression}>... <{foreachelse}>... <{/foreach}>


Attribute Name Type Required Default Description
From Array Yes required N/ Array of circular access
Item String Yes required N/ Variable name of the current element
Key String No N/ Variable name of the current key name
Name String character No N/ Name of the foreach loop used to access the foreach attribute

$ Smarty-> assign ("contacts", array ("phone" => "1", "fax" => "2 ", "cell" => "3"), array ("phone" => "555-4444", "fax" => "555-3333 ", "php Tutorial" => "www.phpddt.com "))); *} {foreach name = outer item = contact from = $ contacts} {foreach key = key item = item from = $ contact} {$ key }:{$ item}
{/Foreach}

3. section for more complex loop traversal

<{Section loop = $ varName [, start = $ start, step = $ setp, max = $ max, $ show = true]}>

Name: section name. $ is not required;

$ Loop: the variable to be cyclic. To the extent, use assign to operate the variable.

$ Start: subscript of the start loop. The default value is 0;

$ Step: increment of the lower mark in each cycle;

$ Show: boolean type. Determines whether to display this block. The default value is true;

<{Section}> attributes;

Index: The subscript of the loop. The default value is 0;

Index_prev: the previous value of the current base object. the default value is-1;

Index_next: the next value of the current underlying object. the default value is 1;

First: whether it is the first next loop;

Last: whether it is the last loop;

Iteration: number of cycles;

Rownum: current row number, alias of iteration;

Loop: The Last loop number. Number of cycles of a Section;

Show: whether to display;

<{Section loop = $ News}>

News No.: <{$ News [loop]. newID}>

News content: <{$ News [loop]. newTitle}>

<{Sectionelse}>

I am sorry

<{/Section}>

For one-dimensional arrays:

{Section name = row loop = $ list}

{$ List. name}

{/Section}

For two-dimensional arrays:

{Section name = row loop = $ list}

{$ List [row]. name}

{/Section}

In addition, you can directly output a single output:

<{$ CommendList.0.infoType}>

<{$ CommendList.1.infoType}>

For 3D arrays:

<{Section name = cate loop = $ myarray}>

<{Section name = scate loop = $ myarray [cate]}>

Id ------> <{$ myarray [cate] [scate]. id}>

Title --------> <{$ myarray [cate] [scate]. title}>

Url ----------> <{$ myarray [cate] [scate]. url}>

<{/Section}>

<{/Section}>

This is the php Tutorial for sorting out smarty control statements!

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.