This article mainly introduces how to obtain data from php by the smarty template engine. It involves the mixed coding techniques of smarty variables and php code. It has some reference value. For more information, see
This article mainly introduces how to obtain data from php by the smarty template engine. It involves the mixed coding techniques of smarty variables and php code. It has some reference value. For more information, see
This article describes how to obtain data from php by using the php/20141105125967.html "> smarty template engine. Share it with you for your reference. The details are as follows:
Variable types that can be assigned by smarty ($ smarty-> assign): All data types supported by php-basic data types, composite data types, and special data types (see the smarty related manual for details ).
Operation/display file: index. php
The Code is as follows:
<? Php
// Create a smarty object
Require_once ("./libs/Smarty. class. php ");
$ Smarty = new Smarty ();
$ Smarty-> assign ("aa", "hello word"); // assign a string
$ Smarty-> assign ("bb", 123); // assign an integer
$ Smarty-> assign ("cc", 90.8); // assign float type and floating point type
$ Smarty-> assign ("dd", true); // assign a string
// Assign an array. The array is generally taken from the database.
$ Arr1 = array ("Beijing", "Shanghai", "Guangzhou"); // Index array
$ Smarty-> assign ("arr1", $ arr1); // assign an Index Array
$ Arr2 = array ("city1" => "Beijing", "city2" => "Shanghai", "city3" => "Guangzhou"); // associate an array
$ Smarty-> assign ("arr2", $ arr2); // assign an associated array
$ Arr3 = array ("Beijing", "Shanghai", "Guangzhou"), array ("Guan Yu", "Zhang Fei", "beauty "));
$ Smarty-> assign ("arr3", $ arr3 );
$ Arr4 = array ("aa" => array ("Beijing", "Shanghai", "Guangzhou"), "bb" => array ("Guan Yu", "Zhang Fei ", "beauty "));
$ Smarty-> assign ("arr4", $ arr4 );
// Object type
Class Master {
Public $ name;
Public $ address;
}
$ Master = new Master ();
$ Master-> name = "Baidu ";
$ Master-> address = "Zhongguancun ";
Class Dog {
Public $ name;
Public $ age;
Public $ color;
Public $ arr;
Public $ master;
Function _ construct ($ name, $ age, $ color, $ arr ){
$ This-> name = $ name;
$ This-> age = $ age;
$ This-> color = $ color;
$ This-> arr = $ arr;
}
}
$ Dog = new Dog ("puppy", 4, "golden yellow", $ arr2 );
$ Dog-> master = $ master;
$ Smarty-> assign ("dog", $ dog );
$ Smarty-> display ("index. tpl ");
?>
Template File: index. tpl
The Code is as follows:
Smarty variable operation
String: {$ aa}
Integer: {$ bb}
Float: {$ cc}
Boolean value: {$ dd}
Array (Index Array): {$ arr1 [0]} -- {$ arr1 [1]} -- {$ arr1 [2]}
Array (associated array): {$ arr2.city1} -- {$ arr2.city2} -- {$ arr2.city3}
Two groups of arrays (index, single): {$ arr3 [0] [0]}
Take two groups of arrays (index, traverse all ):
Two-dimensional array (association): {$ arr4.aa [2]}
Two-dimensional array (join and traverse ):
Object (common attribute): {$ dog-> name}
Object (array attribute): {$ dog-> arr. city1}
Object (Object attribute): {$ dog-> master-> name}
I hope this article will help you with php programming.