PHP template engine Smarty built-in functions foreach, foreachelse Usage Analysis, smartyforeachelse

Source: Internet
Author: User
Tags php define php template

PHP template engine Smarty built-in functions foreach, foreachelse Usage Analysis, smartyforeachelse

This article describes the usage of the PHP template engine Smarty built-in functions foreach and foreachelse. We will share this with you for your reference. The details are as follows:

In the Smarty template, you can use foreach to repeat a block. In the template, an array needs to be allocated from PHP. This array can be a multi-dimensional array. The {foreach} tag in Smarty is the same as the foreach tag in PHP. The difference is that one of them is used in the template file, and the other is used in the PHP script. Therefore, the syntax is different. However, they all work the same way, that is, traversing the content in the array. The {foreachelse} tag is opposite to the {foreach} tag. The {foreachelse} tag is used to execute the content in the tag if the array is empty. The {foreach} and {/foreach} parameters in the template must appear in pairs. They have four parameters, in which the from and item parameters are necessary. For more information about its parameters, see the following list:

Attribute Type Required? Default Value Description
From String Yes N/ Name of the array to be recycled
Item String Yes N/ Variable name of the currently processed Element
Key String No N/

Key name of the currently processed Element

Name String No N/ The name of the loop to access.

We use an instance to demonstrate the use of {foreach} and {foreachelse} in Smarty.

Example: Get the content from the database, assign an array variable $ _ html, assign the array variable to the template, and then traverse the array in the template.

Test. SQL (SQL data used)

---- TABLE structure 'user' -- create table if not exists 'user' ('id' mediumint (8) unsigned not null auto_increment, 'username' varchar (50) not null, 'email 'varchar (50) not null, 'addtime' datetime not null default '2017-00-00 00:00:00 ', primary key ('id ')) ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 7; ---- 'user' -- insert into 'user' ('id', 'username', 'email ', 'addtime') VALUES (1, 'ao', 'canjingkong @ sina.com.cn ', '2017-10-24 00:00:00 ', 'ymhd @ 163.com ', '2017-10-24 00:00:00'), (3, 'chimu qingzi', 'chimiqingzi @ yahoo.com, cn ', '2017-10-24 00:00:00 '), (4, 'Liu Chuan feng', 'lcfeng @ sina.com', '2017-00-00 00:00:00 '), (5, 'crayon shin ', 'labixiaoxin @ sina.com ', '2017-10-24 00:00:00'), (6, 'king Kong gourd wa ', 'jghlw @ sina.com', '2017-10-24 00:00:00 ');

Init. inc. php (template initialization file)

<? Php define ('root _ path', dirname (_ FILE _); // set the website ROOT directory require ROOT_PATH. '/libs/Smarty. class. php '; // load the Smarty template engine $ _ tpl = new Smarty (); // create an instance object $ _ tpl-> template_dir = ROOT_PATH. '/tpl/'; // specify the template directory $ _ tpl-> compile_dir = ROOT_PATH. '. /com/'; // specify the compilation directory $ _ tpl-> left_delimiter =' <{'; // re-specify the left delimiter $ _ tpl-> right_delimiter = '}>'; // re-specify the right delimiter?>

Index. php (main file)

<? Php require 'init. inc. php'; // introduce the template initialization file global $ _ tpl; $ _ mysqli = new mysqli (); // create a mysqli () object $ _ mysqli-> connect ('localhost', 'root', 'database password', 'database name'); // connect to the database, please set $ _ mysqli-> set_charset ('utf8'); // set the encoding $ _ result = $ _ mysqli-> query ("select username, email, addTime from user order by id asc "); $ _ html = array (); while (!! $ _ Row =$ _ result-> fetch_assoc () {$ _ html [] =$ _ row;} $ _ tpl-> assign ('data ', $ _ html); // allocate the array to the template $ _ tpl-> display ('index. tpl '); // introduce the template $ _ mysqli-> close (); // close the database and release resources?>

Tpl/index. tpl (template file of index. php In the main file)

<Html> 

Execution result:

Finally, the $ _ html array passed in index. php of the main file is a two-dimensional array. Reserved variable $ smarty. foreach is used based on the name attribute in the {foreach} tag. The reserved variable attributes used include: first (first record) and last (last record), iteration (always starts from 1 and increases by 1 for each execution), total (used to display the number of cycles)

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.