Smarty loop _ PHP Tutorial

Source: Internet
Author: User
Smarty loop operation. The phpsmarty template engine has two data loops: foreach and section. The following describes the two cycle methods one by one. (1) smartyforeach loop php smarty template engine has two data loops: foreach and section. The following describes the two cycle methods one by one.

(1) smarty foreach loop

Foreach is used to cyclically access an associated array just like a digital index array. Unlike section that can only access a digital index array, foreach syntax is much simpler than section syntax, however, as a compromise, it can only be used for a single array. Each foreach tag must appear in pairs with the close tag/foreach.

The foreach loop has four attributes: from, item, name, key, where from and item are required.

The foreach loop can be nested. the nested foreach names should be different.

When the from attribute has no value, the {foreachelse} segment is executed.

The foreach loop also has its own attribute variables, which can be accessed through {$ smarty. foreach. name. property}, where "name" is the name attribute. For example:

$ Smarty. foreach. name. index current array index, starting from scratch.

$ Smarty. foreach. name. iteration the number of current cycles. different from index, each cycle increases by 1 from 1.

$ Smarty. foreach. name. first is set to TRUE when the current {foreach} loop is in its initial position.

$ Smarty. foreach. name. last is in the final position of the current {foreach} loop. The value is TRUE.

$ Smarty. foreach. name. show is the {foreach} parameter. show is a Boolean value. If the value is FALSE, {foreach} is not displayed. If there is a corresponding {foreachelse}, it will be displayed.

$ Smarty. foreach. name. total includes the number of {foreach} cycles, which can be used in {foreach} or later.

Note: the name attribute is valid only when you need to access the {foreach} Attribute. it is different from {section. Accessing the {foreach} attribute of an undefined name will not throw an error, but it will lead to unpredictable results.

Example:

{Foreach from = $ newsList item = news key = newsId name = news}
Php smarty foreach loop example


{/Foreach}

(2) smarty section loop

{Section name = "sn" loop = "$ BusinesList4 "}

  • {$ Smarty. section. sn. index + 1} {$ BusinesList4 [sn]. 2}

  • {/Section}

    It can be seen that indexes and associated arrays are easy to use. The following describes the attributes in the section:

    1. attributes in the smarty section

    Name: (required) the section Loop name only indicates the unique name of the loop. there is no $ symbol before it;

    Loop: (required) is the name of the variable in the php Declaration. it is used to indicate which array to loop (that is, the name of the array to loop) requires $;

    Start: (optional) initial position of loop execution. if the value is negative, the start position is counted from the end of the array. for example, if there are 7 elements in the array and the value of start IS-2, the index pointing to the current array is 5. the invalid value (beyond the lower limit of the loop array) is automatically adjusted to the closest legal value.

    Step: (optional) for example, a loop in other languages. it is a step. if it is a negative number, it is a reverse loop;

    Max: (optional) maximum subscript of a loop. if it is 1, it will only loop once. if it is 2, it will loop twice;

    Show: (optional) the default value is true. If {sectionelse} is set }. It indicates that this part of content is displayed when the array has no content; if show is false, this part is displayed. If {sectionelse} is not set, this array is not output.

    2. variables in section in smarty

    Index: used to display the index of the current loop, starting from 0 (if the start attribute is specified, it starts with this value), and 1 is added each time (if the step attribute is specified, determined by this value ). if the step and start attributes are not specified, the function of this value is similar to that of iteration, but it starts from 0.

    Index_prev: used to display the index value of the previous loop. The value is-1 at the beginning of the loop.

    Index_next: used to display the value of the next loop index. when the loop is executed until the last time, this value is still 1 greater than the current index value (if step is specified, it depends on this value ).

    Iteration: used to display the number of cycles. unlike the index attribute, which is affected by the start, step, and max attributes, iteration always starts from 1 (index starts from 0 ). rownum is the alias of iteration.

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

    Last: if the current loop is executed until the last time, the last value is set to true.

    Rownum: used to display the number of cycles. This attribute is the alias of iteration. The two are the same.

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

    Show: Is a section parameter. the show value is Boolean, true or false. if this parameter is set to false, the loop is not displayed. if the sectionelse clause is specified, whether or not the clause is displayed depends on the value.

    Total: displays the total number of execution cycles. you can call this attribute in a loop or after execution ends.

    Simple example:

    {Section name = loop = 6}
    Id: {$ smarty. section. loop. index + 1}
    {/Section}

    Smarty section nested loop 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
    • The cycle table in the smarty template contains incomplete td
    • Extension of the for loop in the smarty Template
    • Smarty loops the table and supplements the table with insufficient td information
    • Smarty variable Operator summary
    • Smarty template variable Operator summary
    • Add the latest tag to the information in the smarty Template
    • How to generate a random number in the smarty Template
    • Smarty logical operator number summary

    There are two data loops in the http://www.bkjia.com/PHPjc/764145.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/764145.htmlTechArticlephp smarty template engine: foreach and section loops. The following describes the two cycle methods one by one. (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.