Smarty instance tutorial (2)

Source: Internet
Author: User
Example 3: Some built-in functions in the smarty template are used. Similar functions can be found in the manual. the usage is simple and you can search for them by yourself.

Example 3 use some built-in functions in the smarty template. you can find similar functions in the manual. the usage is very simple and you can search for them by yourself.

 

Example 4. Template control (if/elseif/else/endif)
========================================================== ====================
Example4.tpl
========================================================== ====================



Next we will introduce a section loop block and a foreach loop block. Originally, it should belong to the template part, but because they are the essence of smarty and closely related to the smarty program design part, so let's talk about it separately in this section.

1. foreach: Used to loop a simple array. it is a selective section loop. Its Definition format is:



Next, let's take a look at news. tpl:

1. read this sentence:



Process control in the template


















{Assign var = "tbColor" value = "# D4D0C8 "}Color: {$ tbColor} {Section name = loop = $ News}{If $ tbColor = "# D4D0C8 "} {Assign var = "tbColor" value = "# EEEEEE "}{Else $ tbColor = "# EEEEEE "} {Assign var = "tbColor" value = "# D4D0C8 "}{/If} {/Section}
{$ News [loop]. newsID} {$ News [loop]. newsTitle}


========================================================== ====================
Example4.php
========================================================== ====================
Php code:

 


========================================================== ============
Example4.php output:
========================================================== ============


Process control in the template







































BR>




<
001 1st News
002 2nd news
003 3rd News
004 4th News
005 5th News
006 6th news
007 7th News
008 8th News


Used in the template file:


{If $ tbColor = "# D4D0C8 "}

{Assign var = "tbColor" value = "# EEEEEE "}
{Else $ tbColor = "# EEEEEE "}

{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 code:

--------------------------------------------------------------------------------
{Section name = rows loop = $ data}

{$ Data [rows]}

{/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:
========================================================== ==========
Example5.tpl
========================================================== ==========


Output multiple records in one row












{Section name = loop = $ News step = 1}{If $ smarty. section. loop. index % 4 = 0} {/If} {/Section}
{$ News [loop]. newsID} {$ News [loop]. newsTitle}


========================================================== ================
Example5.php
========================================================== ================

Php code:




========================================================== ============
Example5.php output content:
========================================================== ============



Output multiple records in one row
































001 1st News 002 2nd news 003 3rd News 004 4th News
005 5th News 006 6th news 007 7th News 008 8th News


Note: it can be optimized so that the first line does not output a blank line But the learning procedure is simple and easy to use. here we will explain:


{Section name = loop = $ News step = 1}
{If $ smarty. section. loop. index % 4 = 0}


{/If}
{$ News [loop]. newsID}
{$ News [loop]. newsTitle}
{/Section}

{Section} {/section} refers to a circular part. it will be detailed in the next section. let's take a look at this sentence:


{If $ smarty. section. loop. index % 4 = 0}

$ Smarty. section. loop indicates that the section in the $ smarty instance has a part called loop. it has an attribute called index, which indicates the index value of the current loop, increasing from 0, we compare it with 0 After % 4, that is, if the current index value is a multiple of 4, it outputsOtherwise, execute the following part,
It is easy to solve a very troublesome thing to implement in the program. here I only demonstrate how to use the {if} Statement function. There is also a very convenient way to implement this function in the Smarty template: {cycle}. The example is as follows:




======================================
{Section name = rows loop = $ data}

{$ Data [rows]}

{/Section}
======================================

Posted:
--------------------------------------------------------------------------------

2. --- programming


In the template design section of smarty, I briefly introduced some common settings of smarty in the template. this section mainly introduces How to Start program design in smarty.


First, we will introduce some elements in the. php file we used in the previous section. Similarly, let's take the index. php file at the beginning of the previous section as an example:

========================================================== ==========
Index. php
========================================================== ==========

Php code:

/*************************************** ******
*
* File name: index. php
* For use: Display instance programs
*
* Author: Master
* Email: teacherli@163.com
*
**************************************** *****/
Include_once ("./comm/Smarty. class. php"); // contains the smarty class file

$ Smarty = new Smarty (); // Create a smarty instance object $ smarty
$ Smarty-> template_dir = "./templates"; // set the template directory
$ Smarty-> compile_dir = "./templates_c"; // Set the compilation Directory


// *** Note that,

Here I am a newly added ****//

$ Tpl-> cache_dir = "./cache"; // you can specify a cache directory.
$ Smarty-> cache_lifetime = 60*60*24; // you can specify the cache time.
$ Smarty-> caching = true; // sets the cache mode.

//----------------------------------------------------
// Left and right boundary characters. the default value is {}.
// Conflict, so it is recommended to set it to <{}> or another.
//----------------------------------------------------
$ Smarty-> left_delimiter = "<{";
$ Smarty-> right_delimiter = "}> ";

$ Smarty-> assign ("name", "Li Xiaojun"); // replace Template variables

// Compile and display the index. tpl template under./templates
$ Smarty-> display ("index. tpl ");
?>


/*************************************** ******
*
* File name: example5.php
* For use: Display instance Program 5
*
* Author: Master
* Email: teacherli@163.com
*
**************************************** *****/

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 ("example5.tpl ");
?>


/*************************************** ******
*
* File name: example4.php
* For use: instance Program 4 is displayed.
*
* Author: Master
* Email: teacherli@163.com
*
**************************************** *****/

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 ");
?>

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.