Smarty tutorial 4

Source: Internet
Author: User

Example4.tpl


CODE: [Copy to clipboard] <Head> <title> process control in the template </title> <Body>
<Table border = "1" align = "center">
{Assign var = "tbColor" value = "green "}
Color: {$ tbColor} <br>

{Section name = loop = $ News}
{If $ tbColor = "green "}
<Tr bgcolor = "{$ tbColor}">
{Assign var = "tbColor" value = "orange "}
{Else $ tbColor = "orange "}
<Tr bgcolor = "{$ tbColor}">
{Assign var = "tbColor" value = "green "}
{/If}
<Td >{$ News [loop]. newsID} </td>
<Td >{$ News [loop]. newsTitle} </td>
<Tr>
{/Section}
</Table>
</Body>
</Html>
Example4.php


PHP: [Copy to clipboard]
<? Php
/*************************************** ******
*
* File name: example4.php
* For use: instance Program 4 is displayed.
*
* Author: Master
* Email: teacherli@163.com

* Rectification: forest
**************************************** *****/

Require_once ("./comm/Smarty. class. php ");

$ Smarty = new Smarty ();
$ Smarty-> template_dir = './templates /';
$ Smarty-> compile_dir = './templates_c /';
$ Smarty-> config_dir = './configs /';
$ Smarty-> cache_dir = './cache /';
$ Smarty-> caching = false;

$ Array [] = array ("newsID" => "001", "newsTitle" => "1st News ");
$ Array [] = array ("newsID" => "002", "newsTitle" => "2nd news ");
$ Array [] = array ("newsID" => "003", "newsTitle" => "3rd News ");
$ Array [] = array ("newsID" => "004", "newsTitle" => "4th news ");
$ Array [] = array ("newsID" => "005", "newsTitle" => "5th News ");
$ Array [] = array ("newsID" => "006", "newsTitle" => "6th news ");
$ Array [] = array ("newsID" => "007", "newsTitle" => "7th News ");
$ Array [] = array ("newsID" => "008", "newsTitle" => "8th News ");


$ Smarty-> assign ("News", $ array );

$ Smarty-> display ("example4.tpl ");
?>


Example4.php output:
========================================================== ============
<Html>
<Head> <title> process control in the template </title> <Body>
<Table border = "1">

<Tr bgcolor = "# D4D0C8">

<Td> 001 </td>
<Td> 1st News </td>
</Tr>
<Tr bgcolor = "# EEEEEE">

<Td> 002 </td>
<Td> 2nd News </td>
</Tr>
<Tr bgcolor = "# D4D0C8">

<Td> 003 </td>
<Td> 3rd News </td>
</Tr>
<Tr bgcolor = "# EEEEEE">

<Td> 004 </td>
<Td> 4th News </td>
</Tr>
<Tr bgcolor = "# D4D0C8">

<Td> 005 </td>
<Td> 5th News </td>
</Tr>
<Tr bgcolor = "# EEEEEE">

<Td> 006 </td>
<Td> 6th News </td>
</Tr>
<Tr bgcolor = "# D4D0C8">

<Td> 007 </td>
<Td> 7th News </td>
</Tr>
<Tr bgcolor = "# EEEEEE">

<Td> 008 </td>
<Td> 8th News </td>
</Tr>
</Table>
</Body>
</Html>
========================================================== ==============
Used in the template file:


PHP: [Copy to clipboard]
{If $ tbColor = "# D4D0C8 "}
<Tr bgcolor = "{$ tbColor}">
{Assign var = "tbColor" value = "# EEEEEE "}
{Else $ tbColor = "# EEEEEE "}
<Tr bgcolor = "{$ tbColor}">
{Assign var = "tbColor" value = "# D4D0C8 "}
{/If}

 

Set the background color of each line in this statement block. {assign var = "tbColor" value = "# D4D0C8"} still remembers it, is the definition method for setting the internal variables of the template in Example 3,

Using the built-in flow control statement in the template can greatly improve the control capability of the program. In the following example, a friend of phpx.com asked me to use it
The instance is here for you to learn.
Example 4 I used to describe how to use {if/elseif/else/if}. if you want to implement the barrier, you can use this sentence:


PHP: [Copy to clipboard]
{Section name = rows loop = $ data}
<Tr bgcolor = "{cycle values =" # D4D0C8, # EEEEEE "}">
<Td >{$ data [rows]} </td>
</Tr>
{/Section}

 

Example 5: Use the built-in flow control statement in the template to output a row of multi-cell content, that is, the smarty visually outputs several records for each record:

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.