如何在mustache.php中使用函数包装器? [英] How to use function wrapper in mustache.php?

查看:59
本文介绍了如何在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.

推荐答案

我在

传递的文本是未渲染的文字块.

The text passed is the literal block, unrendered.

但是它提供了Mustache_LambdaHelper,可用于呈现传递的文本.

But it provides a Mustache_LambdaHelper that can be used to render the text passed.

所以我必须将其添加到我的 lambda 函数中:

So I have to add this to my lambda function:

$data->stars = function($label, Mustache_LambdaHelper $helper) {     
  $aux = "";
  $level = $helper->render($label);
  $l = intVal($level);
  for ($i = 0; $i < $l; $i++) {
    $aux .= "+";
  }
  for ($i = $l; $i < 5; $i++) {
    $aux .= ".";
  }
  return $aux;
};

这就是使它工作所需的全部.感谢所有读者!

And that's all it's needed to make it works. Thanks to all readers!

这篇关于如何在mustache.php中使用函数包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆