$_smarty_tpl->tpl_vars['Variable']->value
我想知道如何聪明地访问对象。 smarty_tpl是对象,但是编译后的tpl的代码示例中的属性是什么?它是数组tpl_vars
还是value
还是两者的混合?
在php中,我创建了一个智能对象并将其与方法一起使用(例如,分配和显示)。 在模板本身(文件.tpl)中,我不使用OOP,而是使用类似于html的过程样式。
答案 0 :(得分:0)
我试图做一个小例子,希望能让你明白这个语法:
//A class that represents a fruit basket, a collection of fruits. Similar to $smarty, which is a collection of variables (among other things)
class FruitBasket
{
public $basket = array();
public function foo() {
}
}
//A class that represents a fruit. It has some properties, like 'name' and 'color'
class Fruit
{
public $name = null;
public $color = null;
public function __construct($fruit_name) {
$this->name = $fruit_name;
}
}
$fruits = array(); //Make an array that will hold various fruits
$fruit = new Fruit('banana'); //Create a fruit
$fruit->color = 'yellow'; //Assign a value to its 'color' property
$fruits['banana'] = $fruit; //Assign the fruit to the 'fruits' array
//Make the same steps for 2 more fruits
$fruit = new Fruit('apple');
$fruit->color = 'red';
$fruits['apple'] = $fruit;
$fruit = new Fruit('pear');
$fruit->color = 'green';
$fruits['pear'] = $fruit;
$fruit_basket = new FruitBasket(); //Create a new fruit basket
$fruit_basket->basket = $fruits; //Assign the $fruits array to its 'basket' property
echo $fruit_basket->basket['banana']->color; //prints 'yellow'