如何在mustache.php中使用函数包装器? [英] How to use function wrapper in mustache.php?
问题描述
我开始在PHP上使用Mustache,但我没有设法使包装器功能像债务一样工作.
I'm starting to work with Mustache on PHP and I don't manage to make wrapper functions to work as debt.
我有这个模板
{{#skill_level}}
<span class="stars">
{{#stars}}
{{skill_level}}
{{/stars}}
</span>
{{/skill_level}}
我有这个数据
$data = new StdClass;
$data->skill_level = 3;
$data->stars = function($level) {
$aux = "";
$l = intVal($level);
for ($i = 0; $i < $l; $i++) {
$aux .= "+";
}
for ($i = $l; $i < 5; $i++) {
$aux .= ".";
}
return $aux;
};
我渲染m.render($tenplate, $data);
,我想要获得类似的东西:
I render m.render($tenplate, $data);
and I would like to obtain something like:
<span class="stars">
+++..
</span>
但这是行不通的.
我知道
<span class="stars">
.....
</span>
因为Mustache
将"{{skill_level}}"
而不是值3
传递给了我的函数.
Because Mustache
is passing "{{skill_level}}"
to my function instead of value 3
.
此外,如果我更改模板,则在小胡子标签中放退格键:
Furthermore if I change the template a put backspaces in the mustache labels:
{{ #skill_level }}
<span class="stars">
{{ #stars }}
{{ skill_level }}
{{ /stars }}
</span>
{{ /skill_level }}
然后处理了{{ skill_level }}
,但未将其发送到{{ #starts }}
,获得的渲染为
Then {{ skill_level }}
is processed but it isn't sent to {{ #starts }}
, the render obtained is
<span class="stars">
3
</span>
那么,有人知道我在做什么错吗?我应该如何编写模板才能使其正常工作?欢迎任何建议或经验.谢谢.
So, does anybody know what I'm doing wrong? How should I wrote the template to make it works? Any advice or experience are welcome. Thanks.