Example of smarty loop nesting Usage Analysis and smarty loop nesting examples
This example describes the usage of smarty loop nesting. 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:
<Html>
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.bkjia.com/"); $ smarty-> assign (" $ site_name "," Article 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) & $ 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:
<Html>
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:
<Html>
Focus on $ smarty. section. sec1.rownum: Smarty variable. In the section loop, this variable gets the index value starting from 1. Therefore, when rownum can be split from 2, output </tr> <tr> to change table columns (note! Yes </tr> before <tr> after ). 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, div by, even by, odd
Example:
<!--{if $bigsize ge '650'}--> <!--{else}--> <!--{/if}-->
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.