Smarty template engine fetching data from PHP _php instance

Source: Internet
Author: User

The example in this article describes how the Smarty template engine obtains data from PHP. Share to everyone for your reference. Specifically as follows:

Smarty can assign ($smarty->assign) variable types: All PHP-supported data types-basic data types, composite data types, special data types (see Smarty related manuals).

Action/Display file: index.php

Copy Code code as follows:
<?php
Creating Smarty Objects
Require_once ("./libs/smarty.class.php");
$smarty = new Smarty ();
$smarty->assign ("AA", "Hello word");//allocation string
$smarty->assign ("BB", 123);//Allocate integral type
$smarty->assign ("CC", 90.8);//distribute float type, floating-point type
$smarty->assign ("DD", true);//allocation string
Allocates an array, the array is generally taken out of the database, here directly to the array
$arr 1 = Array ("Beijing", "Shanghai", "Guangzhou");//index array
$smarty->assign ("arr1", $arr 1);//Allocate index array

$arr 2 = Array ("City1" => "Beijing", "City2" => "Shanghai", "City3" => "Guangzhou");//associative array
$smarty->assign ("arr2", $arr 2);//Allocate associative array

$arr 3 = Array ("Beijing", "Shanghai", "Guangzhou"), Array ("Guan Yu", "Zhang Fei", "Beauty"));
$smarty->assign ("Arr3", $arr 3);

$arr 4 = Array ("AA" =>array ("Beijing", "Shanghai", "Guangzhou"), "BB" =>array ("Guan Yu", "Zhang Fei", "Beauty"));
$smarty->assign ("Arr4", $arr 4);

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", $arr 2);
$dog->master = $master;
$smarty->assign ("Dog", $dog);

$smarty->display ("Index.tpl");
?>

Template file: Index.tpl

Copy Code code as follows:

<p style= "Color:green;" > Fetching string: {$aa}</p>
<p style= "color:red;" > Integer: {$bb}</p>
<p style= "Color:blue;" > Fetching floating-point type: {$CC}</p>
<p style= "Color:orange;" > Boolean value: {$dd}</p>
<p style= "Color:indigo;" > Fetching Array (indexed array): {$arr 1[0]}--{$arr 1[1]}--{$arr 1[2]}</p>
<p style= "Color:green;" > Fetching array (associative array): {$arr 2.city1}--{$arr 2.city2}--{$arr 2.city3}</p>
<p style= "color:red;" > Take two arrays (index, single): {$arr 3[0][0]}</p>
<p style= "color:red;" > Take two sets of arrays (index, traverse All):</p>
<p style= "Color:blue;" > Take a two-dimensional Array (association): {$arr 4.aa[2]}</p>
<p style= "Color:blue;" > Take a two-dimensional array (association, traversal):</p>
<p style= "Color:orange;" > Fetching objects (Normal properties): {$dog->name}</p>
<p style= "Color:orange;" > Fetching objects (Array properties): {$dog->arr.city1}</p>
<p style= "Color:orange;" > Fetching objects (Object properties): {$dog->master->name}</p>

I hope this article will help you with your PHP program design.

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.