Smarty loop foreach, detailed description of section instances

Source: Internet
Author: User
Arrays or objects in php can all have for, foreach, while, and listeach loops out, and smarty also has this method. otherwise, php's array object data, smarty does not know how to display it. The usage of for, foreach, and while in php is a habit. at the beginning, the loop array in smarty is always forgotten.

Arrays or objects in php can all have for, foreach, while, and list each to loop out, and smarty also has this method, or else php's array object data, smarty does not know how to display it. The usage of for, foreach, and while in php is a habit. at the beginning, the loop array in smarty always forgets that it is better to remember it than to write it down. The variable symbol is bounded below, and I will use the default braces.

I. foreache, foreacheelse,/foreache method

1. it is another solution for processing loops outside the section (select different solutions based on different needs ). it is used to process a simple array (the element types in the array are the same). Its format is much simpler than section. The disadvantage is that it can only process a simple array.
2. foreach must be used in pairs with/foreach, and the from and item attributes must be specified.


3, foreach can be a trap, but name must be one
4. from: the name of the array to be cyclic, the subscript of the key array, the variable name of the currently processed item, and the name of the loop to be accessed.
5. the foreachelse statement is executed when the array object following the from statement has no value.

II. section, sectionelse,/section method

Section is used to traverse the data in the array. section labels must appear in pairs. the name and loop attributes must be set. the name can be any combination of letters, numbers, and underscores. nesting is allowed, but the nested name must be unique. the variable loop (usually an array) determines the number of times the loop is executed. to output a variable in a section loop, you must add the name variable included in the brackets after the variable. sectionelse is executed when the loop variable has no value.

The parameters that can be included in section are as follows:

1. name indicates the name of a single loop. required options
2. variable name of loop, required
3. KEY value starting from the start loop. the default value is from the first one,
4. step sets the number of hops in a loop. step = 2 only traverses elements with subscripts of 0, 2, and 4.
5. number of max cycles. the default value is 1,
6. does show the loop?

We can see from the above parameters that the subscript is not mentioned, and we often use the array subscript. However, smarty contains.

The following arrayname is the name of a single loop in the section.

A, $ smarty. section. arrayname. index array subscript, the root foreach key is similar

B, $ smarty. section. arrayname. index_prev the subscript root of the previous loop $ smarty. section. arrayname. loop is similar

C, $ smarty. section. arrayname. index_next subscript of the next loop

D, $ smarty. section. arrayname. iteration.

E, $ smarty. section. arrayname. first

F, $ smarty. section. arrayname. last loop

G, $ smarty. section. arrayname. show whether the loop is displayed

H, $ smarty. section. arrayname. total number of cycles

Section to get the values in the array object.

{Section name = arrayname loop = $ array}
{$ Array [arrayname. index]}, {$ array [arrayname]} are equivalent. it can be seen from the beginning. How important is a-h above?

3. Application example

1. one-dimensional array

View copy print?
  1. Array (
  2. "0" => 'home ',
  3. '1' => 'Who ',
  4. '2' => 'tank ',
  5. '3' => 'wh'
  6. );
  7. $ This-> tpl-> assign ("onearray", $ this-> onearray );
array (   "0" => 'home',   '1' => 'who',   '2'=> 'tank',   '3'=> 'what'  );$this->tpl->assign("onearray", $this->onearray);

A), foreach to read

View copy print?
  1. {Foreach from = $ onearray kkey = k item = value}
  2. One-Dimensional key = {$ k} one-dimensional value = {$ value}
  3. {Foreachelse}
  4. Nothing
  5. {/Foreach}
{Foreach from = $ onearray key = k item = value} One-Dimensional key = {$ k} one-dimensional value = {$ value}
{Foreachelse} nothing {/foreach}

The displayed result is

One-Dimensional key = 0 one-dimensional value = home
One-Dimensional key = 1-dimensional value = who
One-Dimensional key = 2-dimensional value = tank
One-Dimensional key = 3-dimensional value = what

B), section to read

View copy print?
  1. {Section name = one loop = $ onearray start = 0 step = 1}
  2. Index = {$ smarty. section. one. index },
  3. Index_prev = {$ smarty. section. one. index_prev },
  4. Index_next = {$ smarty. section. one. index_next },
  5. First = {$ smarty. section. one. first },
  6. Last = {$ smarty. section. one. last },
  7. Iteration = {$ smarty. section. one. iteration },
  8. Total = {$ smarty. section. one. total },
  9. Value = {$ onearray [one]}
  10. {Sectionelse}
  11. Nothing
  12. {/Section}
 {section name=one loop=$onearray start=0 step=1}  index={$smarty.section.one.index},  index_prev={$smarty.section.one.index_prev},  index_next={$smarty.section.one.index_next},  first={$smarty.section.one.first},  last={$smarty.section.one.last},  iteration ={$smarty.section.one.iteration},  total={$smarty.section.one.total},  value={$onearray[one]}
{sectionelse} nothing {/section}

The displayed result is

Index = 0, index_prev =-1, index_next = 1, first = 1, last =, iteration = 1, total = 4, value = home
Index = 1, index_prev = 0, index_next = 2, first =, last =, iteration = 2, total = 4, value = who
Index = 2, index_prev = 1, index_next = 3, first =, last =, iteration = 3, total = 4, value = tank
Index = 3, index_prev = 2, index_next = 4, first =, last = 1, iteration = 4, total = 4, value = what

2. two-dimensional array

View copy print?
  1. Array (
  2. "Test" => 'home ',
  3. '2' => 'Who ',
  4. Array (
  5. "Shanghai ",
  6. "Born" => "Anhui ",
  7. "Name" => "submarine Condor"
  8. ),
  9. Array (
  10. "1583456 ",
  11. "Fax" = & gt; "12345678 ",
  12. "Cell" => "13256478414"
  13. )
  14. );
  15. $ This-> tpl-> assign ("twoarray", $ this-> twoarray );
Array ("test" => 'home', '2' => 'wh', array ("Shanghai", "born" => "Anhui ", "name" => "sea bottom e"), array ("1583456", "fax" => "12345678", "cell" => "13256478414 ")); $ this-> tpl-> assign ("twoarray", $ this-> twoarray );

A), foreach

View copy print?
  1. {Foreach from = $ twoarray kkey = k item = value}
  2. {If is_array ($ value )}
  3. {Foreach from = $ value key = tk item = TV}
  4. Two-dimensional tkey = {$ tk} two-dimensional value = {$ TV}
  5. {Foreachelse}
  6. The two-dimensional array is empty.
  7. {/Foreach}
  8. {Else}
  9. One-Dimensional key = {$ k} one-dimensional value = {$ value}
  10. {/If}
  11. {Foreachelse}
  12. Nothing
  13. {/Foreach}
{Foreach from = $ twoarray key = k item = value} {if is_array ($ value )} {foreach from = $ value key = tk item = TV} two-dimensional tkey = {$ tk} two-dimensional value = {$ TV}
{Foreachelse} the two-dimensional array is null {/foreach} {else} One-Dimensional key = {$ k} one-dimensional value = {$ value}
{/If} {foreachelse} nothing {/foreach}

The result is as follows:

One-Dimensional key = test one-dimensional value = home
One-Dimensional key = 2-dimensional value = who
Two-dimensional tkey = 0 Two-dimensional value = Shanghai
Two-dimensional tkey = born two-dimensional value = Anhui
Two-dimensional tkey = name two-dimensional value = sea table
Two-dimensional tkey = 0 Two-dimensional value = 1583456
Two-dimensional tkey = fax two-dimensional value = 12345678
2d tkey = cell 2d value = 13256478414

B), section

View copy print?
  1. {Section loop = $ twoarray name = two}
  2. {If is_array ($ twoarray [two])}
  3. {Section loop = $ twoarray [two] name = aaa}
  4. Two-dimensional tkey = {$ smarty. section. aaa. index} two-dimensional value = {$ twoarray [two] [aaa]}
  5. {Sectionelse}
  6. The two-dimensional array is empty.
  7. {/Section}
  8. {Else}
  9. One-Dimensional key = {$ smarty. section. two. index} one-dimensional value = {$ twoarray [two]}
  10. {/If}
  11. {Sectionelse}
  12. Nothing
  13. {/Section}
{Section loop = $ twoarray name = two} {if is_array ($ twoarray [two])} {section loop = $ twoarray [two] name = aaa} two-dimensional tkey = {$ smarty. section. aaa. index} two-dimensional value = {$ twoarray [two] [aaa]}
{Sectionelse} the two-dimensional array is null {/section} {else} one-dimensional key ={$ smarty. section. two. index} one-dimensional value ={$ twoarray [two]}
{/If} {sectionelse} nothing {/section}

The result is as follows:

One-Dimensional key = 0 one-dimensional value =
One-Dimensional key = 1-dimensional value =
One-Dimensional key = 2-dimensional value = who
Two-dimensional tkey = 0 Two-dimensional value = Shanghai
Two-dimensional tkey = 1 two-dimensional value =
Two-dimensional tkey = 2 two-dimensional value =

If it is a one-dimensional array with subscripts, and it is not in the order of 0, 1, 2, it will not be able to get anything. Can this be understood as the reason for foreach's existence. However, generally, the data retrieved from the database is a two-dimensional array, and the outer layer starts with 0. Let's look at the example below.

View copy print?
  1. Array (
  2. Array (
  3. 'Name' => 'tank ',
  4. 'Sex' => 'male ',
  5. 'Old' => '28'
  6. ),
  7. Array (
  8. 'Name' => 'job ',
  9. 'Sex' => 'female ',
  10. 'Old' => '123'
  11. )
  12. );
Array ('name' => 'tank', 'sex' => 'male', 'old' => '28 '), array ('name' => 'job', 'sex' => Female, 'old' => '123 '));

Section loop

View copy print?
  1. {Section loop = $ twoarray name = two}
  2. Name = {$ twoarray [two]. name}, sex = {$ twoarray [two]. sex}, old = {$ twoarray [two]. old}
  3. {Sectionelse}
  4. Nothing
  5. {/Section}
 {section loop=$twoarray name=two}  name={$twoarray[two].name},sex={$twoarray[two].sex},old={$twoarray[two].old}
{sectionelse} nothing {/section}

Display result

Name = tank, sex = male, old = 28
Name = joyce, sex = female, old = 111.

Related Article

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.