Distribution data type of the smarty template engine, smarty template data type _ PHP Tutorial

Source: Internet
Author: User
The allocation data type of the smarty template engine, and the smarty template data type. The allocation data type of the smarty template engine. The data type of the smarty template is described in this document. Share it with you for your reference. For details, see the distribution data type of the smarty template engine and the smarty template data type.

This document describes how to use the smarty template engine to allocate data types. Share it with you for your reference. The specific analysis is as follows:

1. allocate basic data

// Allocate basic data $ smarty-> assign ("str", "hello smarty! "); $ Smarty-> assign (" int ", 143); $ smarty-> assign (" double ", 12.1344); $ smarty-> assign (" bool ", true); $ smarty-> assign ("bool2", false); string type: <{$ str}>
Integer: <{$ int}>
Floating point: <{$ double}>
Boolean type: <{$ bool}>
Boolean type false: <{$ bool2}>

Browser Display result:

1 indicates true, 0 indicates false, null indicates false, and nothing is displayed.

2. allocating arrays of composite data

// Index array $ res = array ('Shanghai', 'Beijing', 'shenzhen'); $ smarty-> assign ("arr", $ res ); // join array $ res2 = array ('city1 '=> 'Beijing', 'city2' => 'Guangzhou ', 'city3' => 'hunan '); $ smarty-> assign ("arr2", $ res2); // index a two-dimensional array $ res3 = array ('Xiao Xiao ', 'changshan ', 'Wu Bei'), array ('shanshan', 'changming'); $ smarty-> assign ("arr3", $ res3 ); // associate a two-dimensional array $ res4 = array ('id' => '001', 'name' => 'Zhang San ', 'email '=> 'zhangsan @ mongo3.com'), array ('URL' => 'http: // www.baidu.com ', 'age' => '28 ')); $ smarty-> assign ("arr4", $ res4 ); // associate two-dimensional array 2 $ res5 = array ('emp1' => array ('id' => '001', 'name' => 'zhangsan ', 'email '=> 'zhangsan @ mongo3.com'), 'emp2' => array ('URL' => 'http: // www.baidu.com ', 'age' => '28'); $ smarty-> assign ("arr5", $ res5 );

Template File

Index array: Element 1: <{$ arr [0]}>, element 2: <{$ arr [1]}>, Element 3: <{$ arr [2]}>
Join array method 1 (not recommended): Element 1: <{$ arr2 ['city1 ']}>, element 2: <{$ arr2 ['city2']}>, element 3: <{$ arr2 ['city3']}>
Join array method 2 (recommended): Element 1: <{$ arr2.city1}>, element 2: <{$ arr2.city2}>, Element 3: <{$ arr2.city3}>
Two-dimensional index array: Element 1: <{$ arr3 [0] [0]}>, element 2: <{$ arr3 [0] [1]}>, Element 3: <{$ arr3 [0] [2]}>, Element 4: <{$ arr3 [1] [0]}>, Element 5: <{$ arr3 [1] [1]}>
Join two-dimensional array form 1: id-<{$ arr4 [0]. id}>, name-<{$ arr4 [0]. name }>, email-<{$ arr4 [0]. email }>, url-<{$ arr4 [1]. url }>, age-<{$ arr4 [1]. age}>
Join two-dimensional array form 2: id-<{$ arr5.emp1. id }>, name-<{$ arr5.emp1. name }>, email-<{$ arr5.emp1. email }>, url-<{$ arr5.emp2. url }>, age-<{$ arr5.emp2. age}>

Browser Display result:

3. allocating composite data objects

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 ('001 ', '002 ', '003'); $ master = new Master ('xiaoming ', 22); $ dog1 = new Dog ('White', 1, 'White ', $ arr6, $ master); $ smarty-> assign ("dog", $ dog1 );

Template File

Object:
// Basic attribute name-<{$ dog-> name}>, age-<{$ dog-> age}>, color-<{$ dog-> color}>
// Array attribute arr-<{$ dog-> arr [0]}>, arr-<{$ dog-> arr [1]}>, arr-<{$ dog-> arr [2]}>
// Object property object-<{$ dog-> master-> name}>, object-<{$ dog-> master-> age}>

Browser Display result

I hope this article will help you with php programming.

The following example describes how to use the smarty template engine to allocate data types. Share it with you for your reference. The specific analysis is as follows...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.