PHP Smarty Template variable access Hanshunping explanation

Source: Internet
Author: User
Tags smarty template

Delimiter redefinition prevents errors in Smarty template engine parsing when there is JS CSS code in the template!

Note the point:

The parameters and values in the TPL page template correspond to the relationships in the PHP page.

For example

PHP page code class dog{var $arrT;    function __construct ($arr) {$this->arrt= $arr; }}//One-dimensional indexed array $arra=array (' Beijing ', ' Shanghai ', ' Tianjin ');//Instantiate Object $dog=new Dog ($arrA); $dog->master= $master; $ Smarty->assign ("Dog", $dog), $smarty->display ("Hello.tpl"); TPL page code <{$dog->arrt.city1}>

The member variable ARRT in the Dog class in the PHP page corresponds to the TPL page ARRT

Form parameter $arr of structural method in dog class

function __construct ($arr) {
$this->arrt=$arr;
}

Define the arguments to pass in the parameters of the dog class $arrA $arrA=array (' Beijing ', ' Shanghai ', ' Tianjin ');

Incoming argument $arrA $dog =new Dog ($arrA) when instantiating an object;


Create Smarty Object require_once './libs/smarty.class.php '; $smarty =new Smarty (); $smarty->left_delimiter= "<{"; $ Smarty->right_delimiter= "}>";

Boolean-Type Boolean

PHP page

$smarty->assign ("Yang_boolean", true);

TPL page

<{$yang _string}>

Browser

1

Integer integer

PHP page

$smarty->assign ("Yang_int", 567);

TPL page

<{$yang _int}>

Browser

567

Float type float

PHP page

$smarty->assign ("Yang_float", 56.7);

TPL page

<{$yang _float}>

Browser

56.7

Strings string

PHP page

$smarty->assign ("yang_string", "I Come from China");

TPL page

<{$yang _string}>

Browser

I come from China

Array of arrays one-dimensional indexed arrays

PHP page

$arr =array (' Beijing ', ' Shanghai ', ' Tianjin '); $smarty->assign ("arr1", $arr);

TPL page

<{$arr 1[0]}>| | <{$arr 1[1]}>| | <{$arr 1[2]}>

Browser

Beijing| | shanghai| | Tianjin

Array of arrays one-dimensional indexed arrays

PHP page

$arr 2=array (' city1 ' = ' Beijing ', ' city2 ' = ' Shanghai ', ' city3 ' = ' Tianjin '), $smarty->assign ("arr2", $arr 2);

TPL page

<{$arr 2.city1}>| | <{$arr 2.city2}>| | <{$arr 2.city3}>

Browser

Beijing| | Shanghai | | Tianjin | |

Array of arrays in two-dimensional array (first level is indexed, the second is an indexed array)

PHP page

$arr 3=array ("Beijing", "Shanghai", "Tianjin"), Array ("Song Jiang", "Li Kui", "Lu Junyi")), $smarty->assign ("Arr3", $arr 3);

TPL page

<{$arr 3[0][0]}>| | <{$arr 3[0][1]}>| | <{$arr 3[0][2]}>| | <{$arr 3[1][0]}>| | <{$arr 3[1][1]}>| | <{$arr 3[1][2]}>

Browser

Beijing | | Shanghai | | Tianjin | | Song Jiang | | Li Kui | | Lu Junyi

Array of arrays in two-dimensional array (first layer is indexed array, second layer is associative array)

PHP page

The associative two-dimensional array $arr4=array ("id" = "a001", "name" = "Yang", "Age" = "" "), Array ( "id" = "a002", "name" = "Wang", "Age" = "(") "), $smarty->assign (" Arr4 ", $arr 4);

TPL page

<{$arr 4[0].id}>| | <{$arr 4[0].name}>| | <{$arr 4[0].age}>| | <{$arr 4[1].id}>| | <{$arr 4[1].name}>| | <{$arr 4[1].age}>

Browser

a001| | Yang| | 33| | a002| | Wang| | 23

Array two-dimensional array (first layer is associative array, second layer is associative array)

PHP page

$arr 5=array ("Emp1" =>array ("id" = "a001", "name" = "Yang", "Age" = "") "," emp2 "= >array ("id" = "a002", "name" = "Wang", "Age" = ")"), $smarty->assign ("Arr5", $arr 5);

TPL page

<{$arr 5.emp1.id}>| | <{$arr 5.emp1.name}>| | <{$arr 5.emp1.age}>| | <{$arr 5.emp2.id}>| | <{$arr 5.emp2.name}>| | <{$arr 5.emp2.age}>

Browser

a001| | Yang| | 33| | a002| | Wang| | 23

Array two-dimensional array (first layer is associative array, second layer is indexed array)

PHP page

$arr 6=array ("Emp1" =>array ("a001", "Yang", "I"), "EMP2" =>array ("a002", "Wang", "at")); $sma Rty->assign ("Arr6", $arr 6);

TPL page

<{$arr 6.emp1[0]}>| | <{$arr 6.emp1[1]}>| | <{$arr 6.emp1[2]}>| | <{$arr 6.emp2[0]}>| | <{$arr 6.emp2[1]}>| | <{$arr 6.emp2[2]}>

Browser

a001| | Yang| | 33| | a002| | Wang| | 23

Objects Object

PHP page

class dog{    var  $name;//var is a public variable     var  $age;     var  $color;    var  $arrT;     var $ arr2;    var  $master;//Puppy owner     function __construct ($name, $ Age, $color, $arr, $arr 2) {         $this->name= $name;          $this->age= $age;         $this color= $color;         $this->arrt= $arr;          $this->arr2= $arr 2;    }}//One-dimensional indexed array $arra=array (' Beijing ', ' Shanghai ', ' Tianjin ');//one-dimensional associative array $arrb=array (     ' city1 ' = ' Beijing ',   ' city2 ' = ' Shanghai ',    ' City3 ', ' Tianjin '); $arrC =array (    array (          "id" = "a001",         "Name" = "Yang",          "Age" = "+"     ),     array (          "id" = "a002",         "name" and "Wang",          "Age" = "    ");//Object Assignment class master{     var  $name;    var  $address;} $master =new master (); $master->name= "Jin Sha Harbor"; $master->address= "Xiamen";//Instantiate Object $dog=new dog ("Xiaoming", 5, "red ", $arrA, $arrC), $dog->master= $master, $smarty->assign (" Dog ", $dog), $smarty->display (" Hello.tpl ");

TPL pages ( object members, one-dimensional indexed arrays in objects, two-dimensional arrays in objects, properties of objects in objects)

<{$dog->name}>| | <{$dog->age}>| | <{$dog->color}><{$dog->arrt[1]}><{$dog->arr2[0].name}><{$dog->master->name }>

Browser

Xiao Ming | | 5| | Red Shanghaiyang-Jin Sha Harbor

PHP page (parameter passed in the object is a one-dimensional associative array $arrb)

$arrB =array (' city1 ' = ' Beijing ', ' city2 ' = ' Shanghai ', ' city3 ' = ' Tianjin '); $dog =new Dog ("Xiaoming", 5, "Red", $arrB, $arrC);

TPL page ( one-dimensional associative array in an object )

<{$dog->arrt.city1}>

Browser

Beijing



This article is from the "Jin Sha Harbor" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1843472

PHP Smarty Template variable access Hanshunping explanation

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.