Analysis of smarty loop nesting usage example _ php instance

Source: Internet
Author: User
This article mainly introduces the usage of smarty loop nesting and analyzes the implementation skills and precautions of the Smarty template nested loop based on the instance form, for more information about smarty loop nesting, see the examples in this article. We will share this with you for your reference. The details are as follows:

Test3.php:

<? Phprequire "main. php "; $ forum = array (" category_id "=> 1," category_name "=>" bulletin board ", "topic" => array ("topic_id" => 1, "topic_name" => "website announcement"), array ("category_id" => 2, "category_name" => "文", "topic" => array ("topic_id" => 2, "topic_name" =>" "), array ("topic_id" => 3, "topic_name" => "文 "), array ("category_id" => 3, "category_name" =>" ", "topic" => array ( Array ("topic_id" => 4, "topic_name" => "hardware perimeter"), array ("topic_id" => 5, "topic_name" => "software discussion"); $ tpl-> assign ("forum", $ forum ); $ tpl-> display ("test3.htm");?>

The sample is written as follows:

Templates/test3.htm:

Nested loop test
 
 
  
  
<{Section name = sec1 loop = $ forum}>
  
  <{Section name = sec2 loop = $ forum [sec1]. topic}> 
   <{/Section }>< {/section}> 
  
<{$ Forum [sec1]. category_name}>
<{$ Forum [sec1]. topic [sec2]. topic_name}>

Test2.php:

<? Phprequire_once ('. /include/db_fns.php '); include_once (". /Smarty/libs/Smarty. class. php "); // contains the Smarty class file $ smarty = new Smarty (); // creates a Smarty instance object $ Smarty $ smarty-> template_dir = ". /templates/dedecms "; // set the template directory $ smarty-> compile_dir =" templates/templates_c "; // Set the compilation directory $ smarty-> assign (" template_url ",". /"); $ smarty-> assign (" $ site_url "," http://www.php.net/ "); $ Smarty-> assign (" $ site_name "," Document Management System "); $ smarty-> left_delimiter =" <{"; // Set the left boundary $ smarty-> right_delimiter = "}>"; // set the right boundary to $ db_conn = db_connect (); $ query = "SELECT cat_ID, cat_name FROM categories order by cat_ID DESC "; $ result = mysql_query ($ query); $ I = 5; while ($ row = mysql_fetch_array ($ result )) & amp; $ I> 0) {$ query2 = "SELECT ID, post_title, post_date FROM post WHERE post. post_category = $ row [cat_ID] AND post_status <> 'unpbulish' order by post_date DESC "; $ result2 = mysql_query ($ query2); $ I = 5; while ($ row2 = mysql_fetch_array ($ result2 )) & amp; $ I> 0) {$ row2 [post_date] = date ('M-D', strtotime ($ row2 [post_date]); $ category = array ("cat_ID" => "$ row [cat_ID]", "cat_name" => "$ row [cat_name]", "post" => array ("ID" => "$ row2 [ID]", "post_title" => "$ row2 [post_title]", "post_category" => "$ row2 [post_category]", "post _ Date "=>" $ row2 [post_date] "); $ I -- ;}$ smarty-> assign (" forum ", $ category ); $ smarty-> display ("test2.htm");?>

Test2.htm:

    Nested loop test      
 
 
  
  
<{Section name = sec1 loop = $ forum}>
  
  
        
        
   <{Section name = sec2 loop = $ forum [sec1]. post}> 
   
        
        
        
   <{/Section }>< {/section}> 
  
<{$ Forum [sec1]. cat_id}>
<{$ Forum [sec1]. post [sec2]. post_title}>

Test4.php:

<?phprequire "main.php";$my_array = array(array("value" => "0"),array("value" => "1"),array("value" => "2"),array("value" => "3"),array("value" => "4"),array("value" => "5"),array("value" => "6"),array("value" => "7"),array("value" => "8"),array("value" => "9"));$tpl->assign("my_array", $my_array);$tpl->display('test4.htm');?>

The template is written as follows:

Templates/test4.htm:

Horizontal repeat table test
 
 
     <{Section name = sec1 loop = $ my_array}> 
    <{If $ smarty. section. sec1.rownum is p by 2}> 
   
     <{/If }>< {/section}> 
   
<{$ My_array [sec1]. value}>

The focus is on the smarty variable $ Smarty. section. sec1.rownum. in the section loop, this variable will get the index value starting from 1. Therefore, when rownum can be split from 2, it will outputChange Table columns (note! YesIn front). Therefore, number 2 is the number of data records we want to present in a column. You can change other different rendering methods.

The following operators are available::

Eq, ne, neq, gt, lt, lte, le, gte, ge, is even, is odd, is not even, is not odd, not, mod, p by, even by, odd

Example:

    " border="0" width="650" class="product_photo" />
     " border="0" class="product_photo" />
 

Smarty was not commonly used in the past. it was interesting to have a friend's website changed over the past two days.

Capture description:

The capture function is used to collect the data output from the template to a variable instead of outputting the data to the page.

Any data between {capture name = "foo"} and {/capture} is received from the variable specified by the function name attribute ($ foo ).

The collected information can be used in the special variable $ smarty.

For example, capture. foo collects the above data. if the function does not have a name attribute, "default" will be used ".

Each {capture} must correspond to {/capture}, and the capture function cannot be nested.

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.