0
$_smarty_tpl->tpl_vars['Variable']->value

我想知道 smarty 如何访问对象。smarty_tpl 是对象,但是编译的 tpl 的代码示例中的属性是什么?是数组tpl_vars还是value两者的混合?

在 php 中,我创建了一个 smarty 对象并将其与方法一起使用(例如分配和显示)。在模板本身(文件 .tpl)中,我不使用 OOP,而是使用类似于 html 的程序样式。

4

1 回答 1

1

我试着做一个小例子,希望能让你明白这个语法:

//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'
于 2013-01-11T06:47:56.927 回答