handlebar.js访问lambda函数中的对象

时间:2018-04-25 08:11:52

标签: handlebars.js mustache

我试图访问lamda函数中的嵌套对象。该对象看起来类似于:

{
  foo: function(){
    return {
      'bar': "success"
    }
  }
}

当我尝试使用{{foo.bar}}访问它时,它返回一个空字符串。如果我用{{#foo}}{{bar}}{{/foo}}包装它就可以了。这是故意的吗?有什么方法可以让第一个声明有效吗?这些模板已经在使用了mustache.php,我也尝试在前端工作。

以下是该问题的工作演示:



var obj = {
  foo: function(){
    return {
      'bar': "success"
    }
  }
};

var source   = document.getElementById("template").innerHTML;
var template = Handlebars.compile(source);
document.getElementById("output").innerHTML = template(obj);

<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.js"></script>

<script id="template" type="text/x-handlebars-template">
nested: {{#foo}}{{bar}}{{/foo}} <br/>
dot notation: {{foo.bar}}
</script>

<div id="output"></div>
&#13;
&#13;
&#13;

0 个答案:

没有答案