Smarty Cycle Operation _php Tutorial

Source: Internet
Author: User
Tags smarty template
The PHP smarty template engine has two ways of data looping, namely foreach and section loops. The following one by one describes these two types of loops.

(1) Smarty foreach Loop

foreach is used to iterate through an associative array like iterating over a numeric index array, unlike a section with only access to a numeric index array, the syntax of foreach is much simpler than the syntax of a section, but it can only be used as a compromise for a single array. Each foreach tag must appear in pairs with the close tag/foreach.

The Foreach Loop has four properties, From,item,name,key, where the From and item are required.

The Foreach loop can be nested, and the names of the nested foreach should be different.

The {Foreachelse} fragment is executed when the From property has no value.

The Foreach loop also has a variable of its own property, which can be accessed through {$smarty. Foreach.name.property}, where "name" is the Name property. For example:

$smarty. Foreach.name.index the current array index, starting at zero.

$smarty. Foreach.name.iteration the current number of cycles, which, unlike index, starts at 1 and increases by 1 per cycle.

$smarty. Foreach.name.first in the current {foreach} loop at the initial position value of true.

$smarty. Foreach.name.last in the current {foreach} loop in the final position is a value of true.

$smarty. Foreach.name.show Show is a parameter of {foreach}. Show is a Boolean value. If the value is False,{foreach} will not be displayed. If there is a corresponding {foreachelse}, it will be displayed.

$smarty. Foreach.name.total total includes the number of times {foreach} will be cycled, either in {foreach} or later.

Note: The Name property is only valid when the {foreach} property needs to be accessed, unlike {section}. Accessing a {foreach} property that does not have a name defined does not throw an error, but will result in unpredictable results.

Example:

{foreach from= $newsList item=news key=newsid Name=news}
PHP Smarty Foreach Loop example


{/foreach}

(2) Smarty section cycle

{section name= "sn" loop= "$BusinesList 4"}

  • {$smarty. section.sn.index+1} {$BusinesList 4[sn].2}

  • {/section}

    As you can see, it is convenient to use both an index and an associative array. Descriptions of the properties in the following sections are described below:

    1. Attributes in Smarty section

    Name: (required) is a section loop that is named just to indicate that the only name of the loop is not particularly meaningful, preceded by a $ symbol;

    Loop: (required) is the variable name in the PHP declaration, which is used to indicate which array (that is, the array name to loop) needs to be used;

    Start: (optional) The initial position of the loop execution. If the value is negative, the start position is calculated from the end of the array. For example, if there are 7 elements in the array, specifying start IS-2, then the index to the current array is 5. Illegal values (exceeding the lower bounds of the loop array) are automatically adjusted to the nearest legal value.

    Step: (optional) such as other languages of the loop, is a step, if negative, then the reverse cycle;

    Max: (optional) The maximum index of the loop, if it is 1, loop only 1 times, if 2 then loop 2 times;

    Show: (optional) The default is true to display. If {Sectionelse} is set. Indicates that the contents of this section are displayed if the array has no content, and if show is false, this section is displayed. If {Sectionelse} is not set, the array is not output.

    2. Variables in section Smarty

    Index: Used to display the indexes of the current loop, starting at 0 (the value starts if the Start property is specified), plus 1 per time (if the step attribute is specified, the value is determined). If you do not specify the step and start properties, this value is similar to iteration. Just starting from 0.

    Index_prev: Used to display the previous loop index value. At the beginning of the loop, this value is-1.

    Index_next: Used to display the next loop index value. When the loop executes to the last time, this value is still 1 larger than the current index value (if step is specified, depending on this value).

    Iteration: The number of times to display the loop. Iteration Unlike the Index property, which is affected by the start, step, and Max properties, the value always starts at 1 (index is from 0). RowNum is the alias of iteration, the two equals.

    First: If the current loop is executed for the first time, it is set to true.

    Last: If the current loop is executed to the end of the previous one, it is set to true.

    RowNum: The number of times to display the loop. The attribute is an alias of iteration, which is equivalent to both.

    Loop: The index value that is used to display the last loop of the loop. The value can be used inside the loop or after the loop ends.

    Show: is the parameter of section. Show evaluates to a Boolean value of TRUE or false. If set to False, the loop will not be displayed. If the SECTIONELSE clause is specified, whether the sentence is displayed depends on the value.

    Total: Used to show how many times the loop has been executed. You can call this property either in a loop or after the execution ends.

    Simple example:

    {Section Name=loop loop=6}
    ID: {$smarty. section.loop.index+1}
    {/section}

    Smarty section Nested Loops Example:

    {section name= "sn" loop= "$dis _ctype"}
    {$BusinesList =${$dis _ctype[sn].2}}
    {section name=sn2 loop= $BusinesList}

  • {$smarty. section.sn2.index+1} {$BusinesList [sn2].2}

  • {/section}
    {/section}

    Articles you may be interested in

      • TD in Smarty template with circular form complement
      • Smarty extension for loop in template
      • Smarty Cycle form and supplement form TD shortage situation
      • Smarty Variable operator Summary
      • Smarty Template Variable operator summary
      • Add latest tags to information in smarty templates
      • How to generate random numbers in a smarty template
      • Summary of Smarty logical operation symbols

    http://www.bkjia.com/PHPjc/764145.html www.bkjia.com true http://www.bkjia.com/PHPjc/764145.html techarticle The PHP smarty template engine has two ways of data looping, namely foreach and section loops. The following one by one describes these two types of loops. (1) Smarty foreach Loop ...

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