余烬模型字段中的车把代码.如何评估

Handlebars code inside of Ember Model field. How to evaluate?

本文关键字:何评估 评估 代码 字段 模型 余烬      更新时间:2023-09-26

假设我有一些带有html字段的模型。此字段包含一些车把代码。例如

<div class="foo">
  {{model.title}}
</div>

问题是当我尝试迭代模型并渲染html字段时,它不会评估其中的车把代码。

{{#each models as |model|}}
  {{{model.html}}}
{{/each}}
这里有

一个想法,创建一个车把助手,它将编译车把。

通过此示例,我相信您可以构建适合您需求的内容:

<script type="text/javascript" src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars.min-latest.js"></script>
<script>
  var data = [{
    "title": "Hey",
    "subtitle": "I'm an inner handlebar template.",
    "html": '<h1>{{title}}</h1><h2>{{subtitle}}</h2>'
  }, {
    "title": "Nice!",
    "html": '<h1>{{title}}</h1>'
  }];
  
  /** Handlebar helper that will compile the content passed, with the data at index **/
  Handlebars.registerHelper("compile", function(content, index) {
    var template = Handlebars.compile(content);
    return template(data[index]);
  });
  var content = '{{#each .}}'
    {{{compile html @index}}}'
  {{/each}}';
  var template = Handlebars.compile(content);
  
  document.body.innerHTML = template(data);
</script>

我从未使用过余烬,但我相信您可以轻松使用它;)