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