Twig lets you pass array or object to the template and gives you the same interface to access members for both data structures.
So, for example:
$test = array('foo' => 'foo', 'bar' => 'bar');
Will let you access this in template as test.foo and test.bar
Now instance of this object will have the same effect. Which is very awesome 🙂
class test
{
public $foo;
public $bar;
}
How about an object that uses magic __set and __get methods?
class test
{
public $properties;
public function __set($name, $value)
{
$this->properties[$name] = $value;
}
public function __get($name)
{
return $this->properties[$name];
}
}
Unfortunately in this case you can’t access properties of this object.
I’m not sure if the Twig is the issue here or PHP itself.
You need to implement
__isset()as well, egSee http://twig.sensiolabs.org/doc/recipes.html#using-dynamic-object-properties