We all know that smarty is used to write templates, but the data written to the template can be broadly divided into the following:
Integer decimal string boolean Array (one-dimensional index, one-dimensional associative array, two-dimensional array, two-dimensional associative array, etc.) objects.
Here is my instance code, which has been tested for each variable type:
<?phprequire_once './libs/smarty.class.php '; $smarty =new Smarty (); $smarty->left_delimiter= "<{"; $smarty- >right_delimiter= "}>", $smarty->assign ("var1", "Happy"), $smarty->assign ("Var2", 333); $smarty Assign ("Var3", 3.1415926); $smarty->assign ("VAR4", true);//arrays are generally taken out of the database $arr1=array (' Beijing ', ' Shanghai ', ' Tianjin '); $smarty- >assign ("Var5", $arr 1), $arr 2=array (' city1 ' = ' Beijing ', ' city2 ' = ' Shanghai ', ' city3 ' = ' Tianjin '); $smarty->assign (" Var6 ", $arr 2); $arr 3=array (Array (' Beijing ', ' Shanghai ', ' Tianjin '), Array (' Song Jiang ', ' Lu Junyi ', ' Zhang Fei ')), $smarty->assign (" Var7 ", $arr 3); $arr 4 = Array (' city1 ' = ' Beijing ', ' city2 ' and ' Shanghai ', ' city3 ' and ' Tianjin '), Array (' person1 ' = ' jiang ', ' person2 ' = ' Lu Junyi ', ' Person3 ' = ' Zhang Fei '); $smarty->assign ("Var8", $arr 4); $arr 5=array (' emp1 ' =>array (' city1 ' = ' Beijing ', ' city2 ' = > ' Shanghai ', ' city3 ' and ' Tianjin ', ' emp2 ' =>array (' person1 ' = ' song Jiang ', ' person2 ' = ' Lu Junyi ', ' person3 ' = ' Zhang Fei ')); $ Smarty->assign ("Var9", $arr 5), Class Dog{var $name, Var $age, var $color; var $arr; function __construct ($name, $age, $ Color, $arr) {$this->name= $name, $this->age= $age, $this->color= $color; $this->arr= $arr;}} $dog _1=new Dog (' Small white ', ' yellow ', $arr 1), $smarty->assign ("Var10", $dog _1), $dog _2=new dog (' Little black ', ' a ', ' dark ', $arr 2); $smarty->assign ("Var11", $dog _2), echo "<pre>";p Rint_r ($_server); echo "</pre>";//indicates which template the object uses. $smarty->display ("Test.tpl");? >The above Smarty object creation and data allocation implementation, the following is its corresponding template:
<{config_load file= '. /config/my.conf '}><body bgcolor= "<{#bgcolor #}>" >They have the corresponding specifications in the template.Here's a summary:
General data types, such as integers, decimals, strings, and so on, can be used directly.
If it is an array type, it is divided into two kinds: that is, there is association and no association, the former is to use the next point number, the latter is directly used in the way of the array, and so on.
The object is specified in the "-to" manner.
If it is a compound type of data, it should be based on the above principles.
Basic operation of Smarty template engine for various kinds of data