How the smarty template engine obtains data from php

Source: Internet
Author: User
Tags smarty template
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.

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.