小胡子不评估函数内部的{{}} [英] Mustache doesn't Evaluate {{}} inside function

查看:116
本文介绍了小胡子不评估函数内部的{{}}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有小胡子的moment.js格式化JS Date()对象,但是小胡子不会将评估值传递给函数.

I am trying to format JS Date () object with moment.js with mustache, however mustache doesn't pass evaluated value to function.

在主干视图中:

render: function () {
    var user = this.user.toJSON ();  //model

    _.extend (user, {formatLastLoginAt: this.formatLastLoginAt});

    var rendered = mustache.render (template, user);
    this.$el.html (rendered);

    return this;
},

formatLastLoginAt: function () {
   return function (lastLoginAt) {
     return moment (lastLoginAt).format ('Do MMMM YYYY');
  }
}

用户对象绑定:

在模板中:

{{#lastLoginAt}}
    <tr>
     <td>Last Login:</td> 
     <td>{{#formatLastLoginAt}}{{lastLoginAt}}{{/formatLastLoginAt}}</td>
    </tr>
{{/lastLoginAt}}

moment.js给出NaN错误,因为'lastLoginAt'作为原义字符串"{{lastLoginAt}}"而不是其Date ()值传递.

moment.js gives NaN error as 'lastLoginAt' pass in as literal string "{{lastLoginAt}}" rather than its Date () value.

尝试过moment ().format (),它可以工作.因此,lambda构造应该可以,并且{{#lastLoginAt}}是非空的.

Tried with moment ().format (), it works. Thus the lambda construct should be ok and {{#lastLoginAt}} is non-empty.

我错过了什么吗?感谢您的建议.谢谢.

Anything I missed out? Appreciate your advice. Thank you.

推荐答案

小胡子不会为您呈现内容.您的函数接受一个参数lastLoginAt,但Mustache将为您传递另一个参数:render.用lastLoginAt调用render将扩展变量:

Mustache won't render the contents for you. Your function takes one argument, lastLoginAt, but Mustache will pass you another: render. Calling render with lastLoginAt will expand the variable:

formatLastLoginAt: function () {
   return function (lastLoginAt, render) {
     lastLoginAt = render(lastLoginAt);  // expand variable
     return moment (lastLoginAt).format ('Do MMMM YYYY');
  }
}

这篇关于小胡子不评估函数内部的{{}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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