Smarty built-in function foreach usage instance, smartyforeach_PHP tutorial

Source: Internet
Author: User
Smarty built-in function foreach usage instance, smartyforeach. Smarty built-in function foreach usage example. smartyforeach this article describes the usage of the smarty built-in function foreach. Share it with you for your reference. Details: output file: index. p smarty built-in function foreach usage instance, smartyforeach

This example describes the usage of the smarty built-in function foreach. Share it with you for your reference. The details are as follows:

Output file: index. php

The code is as follows:

<? Php
Require_once ('libs/Smarty. class. php ');
$ Smarty = new Smarty ();
$ Smarty-> setTemplateDir ($ _ SERVER ['document _ root']. "/php/templates /");
$ Smarty-> setCompileDir ($ _ SERVER ['document _ root']. "/php/templates_c /");
$ Smarty-> setCacheDir ($ _ SERVER ['document _ root']. "/php/cache /");
$ Smarty-> caching = false;

$ Name = array ("Xiyangyang", "Meiyang", "lazy", "hahaha ");
$ Family = array ("husband" => "Xiyang", "wife" => "Meiyang", "boy" => "lazy ", "girl" => "hahaha ");
$ Smarty-> assign ("name", $ name );
$ Smarty-> assign ("family", $ family );

$ Smarty-> display ("temp.htm ");
?>

Template File: temp.htm

The code is as follows:

{Foreach $ name as $ value}
{$ Value}
{/Foreach}


{Foreach $ family as $ family_name}
{$ Family_name @ key }={$ family_name}
{/Foreach}


{Foreach from = $ family item = f_name key = k}
{$ K }={ $ f_name}
{/Foreach}

The output method of the array can be roughly two forms:

(1) one is {foreach from = array variable name item = Array Value} of smarty 2 }.
(2) the output method of smarty 3 is the same as that of php. the above instance does not add $ key. when a key name needs to be output, @ key is used in the form of an instance; you can use $ key => $ value.

In addition, if only one value in the array is obtained, it can be output directly, for example, the {$ family. husband} of the associated array and the {$ name [0]} of the index array.

I hope this article will help you with php programming.

Examples in this article describes the usage of the smarty built-in function foreach. Share it with you for your reference. Details: output file: index. p...

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.