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 ...