Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement smarty template engine to assign data types

Shulou Source: shulou.com Published: 2022-06-03 09:22:14 11月15日 Update

This article mainly explains "how to achieve smarty template engine allocation of data types", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to achieve smarty template engine allocation of data types" bar!

The specific analysis is as follows:

I. Distribution of basic data

/ / assign basic data $smarty- > assign ("str", "hello smarty!"); $smarty- > assign ("int", 12.1344); $smarty- > assign ("double", 12.1344); $smarty- > assign ("bool", true); $smarty- > assign ("bool2", false); string type: integer: floating point: Boolean true: Boolean false:

The browser displays the results:

1 for true, 0 for false, false for null, showing nothing.

Second, allocate the array of composite data

/ / indexed array $res=array ('Shanghai', 'Beijing', 'Shenzhen'); $smarty- > assign ("arr", $res); / associative array $res2=array ('city1'= >' Beijing', 'city2'= >' Guangzhou', 'city3'= >' Hunan'); $smarty- > assign ("arr2", $res2); / / indexed 2D array $res3 = array ('Xiaoxiao', 'Changshan','Wu Bei'), array ('Shanshan', 'Chang Ming') $smarty- > assign ("arr3", $res3); / / the associated two-dimensional array $res4 = array (array ('id'= >' 001), email'= > 'zhangsan@1163.com'), array (' url'= > 'http://www.baidu.com','age'=>'28')); $smarty- > assign ("arr4", $res4) / / an associative 2D array of 2 $res5=array ('emp1'= > array (' id'= > '001'), email'= > 'zhangsan@1163.com'),' emp2'= > array ('url'= >' http://www.baidu.com','age'=>'28')); $smarty- > assign ("arr5", $res5)

Template file

Indexed array: element 1, element 2, element 3: associative array 1 (not recommended): element 1, element 2, element 3: associative array, element 2 (recommended): element 1, element 2, element 3: two-dimensional indexed array: element 1, element 2, element 3: element 1, element 2, element 3, element 4, element 5: associative two-dimensional array form 1: id-, name-, email- Url-, age- associative two-dimensional array form 2: id-, name-, email-, url-, age-

The browser displays the results:

Third, the object of assigning compound data

Class Master {var $name; var $age; function _ _ construct ($name,$age) {$this- > name=$name; $this- > age=$age;}} class Dog {var $name; var $age; var $color; var $arr; var $master; function _ construct ($name,$age,$color,$arr6,$master) {$this- > name=$name; $this- > age=$age; $this- > color=$color; $this- > arr=$arr6; $this- > master=$master;} $arr6=array $dog1 = new Dog ('Xiaobai', 1MagneWhitekeeper Magnum Arr6 magic master); $smarty- > assign ("dog", $dog1)

Template file

Object: / / basic properties name-name} >, age-age} >, color-color} > / / array properties arr-arr [0]} >, arr-arr [1]} >, arr-arr [2]} > / / object properties object-master- > name} >, object-master- > age} >

The browser displays the results

Thank you for your reading, the above is "how to achieve smarty template engine allocation of data types" content, after the study of this article, I believe you on how to achieve smarty template engine allocation of data types of this problem has a deeper understanding, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Elements arrays data assignments types associations templates 2D engines indexes objects properties browsers results learning browsing content Boolean forms files Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple Shulou Tech Info MariaDB Redmi