Smarty built-in function foreach usages example, smartyforeach_php tutorial

Source: Internet
Author: User

Smarty built-in function foreach usage instance, Smartyforeach


This example describes the Smarty built-in function foreach usage. Share to everyone for your reference. Specific as follows:

Output file: index.php
Copy the Code code 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 ("Radiant", "Beautiful Yangyang", "lazy", "hahaha");
$family = Array ("husband" = "radiant", "wife" and "Mei Yang", "boy" = "lazy", "Girl" and "Ha Ha");
$smarty->assign ("name", $name);
$smarty->assign ("Family", $family);

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

Template file: temp.htm
Copy the Code Code 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}

There are roughly two ways to output an array:

(1) A {foreach from= array variable name item= array value} that is Smarty 2.
(2) The output method of the Smarty 3 is consistent with PHP, the above instance does not add $key, need to output the key name, use the form of the example @key; you can use $key=> $value this form.

In addition, if you take only one of the values in the array, you can output directly, such as {$family. Husband} of the associative array, {$name [0]} of the indexed array, and so on.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/946756.html www.bkjia.com true http://www.bkjia.com/PHPjc/946756.html techarticle Smarty Built-in function foreach usages, Smartyforeach This example describes the Smarty built-in function foreach usage. Share to everyone for your reference. The following: 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.