【发布时间】:2014-08-05 21:13:00
【问题描述】:
我想让 Mustache 模板引用一个局部,其中局部也将数据添加到上下文中。而不必将数据中的数据定义到初始的 Mustache 渲染中。
我在https://gist.github.com/lode/ecc27fe1ededc9b4a219 有一个模型
归结为:
<?php
// controller
$options = array(
'partials' => array(
'members_collection' => new members_collection
)
);
$mustache = new Mustache_Engine($options);
$template = '
<h1>The team</h1>
{{> members_collection}}
';
echo $mustache->render($template);
// viewmodel
class members_collection {
public $data;
public function __toString() {
$template = '
<ul>
{{# data}}
{{.}}
{{/ data}}
</ul>
';
$mustache = new Mustache_Engine();
return $mustache->render($template, $this);
}
public function __construct() {
$this->data = array(
'Foo Bar',
'Bar Baz',
'Baz Foo',
);
}
}
这会产生类似Cannot use object of type members_collection as array 的错误。
有没有办法让它工作?还是使用__toString 不是正确的方法?并且会使用 partials_loader 或 __invoke 帮助吗?我得到了它,但可能会错过一些东西。
【问题讨论】:
标签: php viewmodel mustache.php