Example Analysis _php instance of Smarty Cyclic embedding method

Source: Internet
Author: User

The example in this article describes the Smarty cyclic nesting method. Share to everyone for your reference, specific as follows:

test3.php:

<?php
require "main.php";
$forum = Array (
    array ("category_id" => 1, "Category_name" => "Bulletin Area",
      "topic" => Array (
        array ("topic _id "=> 1, topic_name" => "Station Bulletin"))
    ,
    Array ("category_id" => 2, "Category_name" => "literary Zone",
      "topic" => Array (
        "topic_id" => 2, "topic_name" => "Good Book Introduction"),
        Array ("TOPIC_ID" => 3, " topic_name "=>" Chiven))
    ,
    Array ("category_id" => 3, "Category_name" => "Computer Zone",
      "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 version is as follows:

Templates/test3.htm:

 
 

test2.php:

<?php require_once ('./include/db_fns.php '); Include_once ("./smarty/libs/smarty.class.php"); Contains the Smarty class file $smarty = new Smarty (); Establish Smarty instance Object $smarty $smarty->template_dir = "./templates/dedecms";//Set template directory $smarty->compile_dir = "templates /templates_c ";
Sets the compilation directory $smarty->assign ("Template_url", "./");
$smarty->assign ("$site _url", "http://www.jb51.net/");
$smarty->assign ("$site _name", "Article Management System"); $smarty->left_delimiter = "<{"; Set the left border character $smarty->right_delimiter = "}>";
Set the right border character $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) {$query 2= "select ID, Post_title, post_date From post WHERE post.post_category = $row [cat_id] and post_status <> ' Unpbu
        Lish ' ORDER by post_date DESC ';
        $result 2=mysql_query ($query 2); 
    $i = 5;    while ($row 2 = mysql_fetch_array ($result 2)) && $i > 0) {$row 2[post_date]=date (' m-d ', St
            Rtotime ($row 2[post_date])); $category = Array ("cat_id" => "$row [cat_id]", "Cat_name" => "$row [Cat_name]", "POST" =>array ("ID" => "$row
            2[id] "," Post_title "=>" $row 2[post_title] "," Post_category "=>" $row 2[post_category] ",
            "Post_date" => "$row 2[post_date]");
        $i--;
}} $smarty->assign ("forum", $category);

 $smarty->display ("test2.htm");?>

Test2.htm:

 
 

test4.php:

<?php
require "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:

 
 

The point is $smarty. section.sec1.rownum this smarty variable, which gets the index value starting at 1 in the section loop, so when rownum can be removed by 2, it outputs </tr><tr> Make a Table change column (note!) Is </tr> in front <tr> in the back). So the number 2 is the number of pens we want to render in a column. You can change different ways of presenting.

operators have the following :

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 by

Example:

<!--{if $bigsize ge ' 650 '}-->
    "border=" 0 "width=" 650 "class=" Product_photo "/>
<!--{else}-->
     "border=" 0 "class=" Product_photo "/>
<!--{/if}-->

Formerly not commonly used smarty, these two days have a friend's website to change, conveniently use a bit, still quite interesting.

Notes on Capture:

The role of the capture function is to collect data from the template output into a variable, rather than outputting them to the page.

Any data between {capture Name= "foo"} and {/capture} is received in the variable specified by the function's Name property ($foo).

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

For example, Capture.foo collects the above data. If the function does not have a name attribute, it will use "default".

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

More interested in smarty related content readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on Smarty template.

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.