把手在变量中展开json并进行评估

时间:2022-04-02 23:29:52

I have a handlebars template that is passed a JSON object. I can render all that fine but some of the strings actually contain template information such that when I render it I get something like this:

我有一个传递JSON对象的把手模板。我可以渲染所有这些,但是一些字符串实际上包含模板信息,这样当我渲染它时,我得到这样的东西:

index.handlebars snippet:

    {{#each json}}
      <td> {{tooltip}}</td>
    {{/each}}

Output (where e1 and f1 are parts of the json object):

输出(其中e1和f1是json对象的一部分):

Blah, blah {{ e1 }} {{ f1 }} blah blah

Blah,等等{{e1}} {{f1}}等等

Is there any way to get handlebars to expand the variable and evaluate it within its context?

有没有办法让把手扩展变量并在其上下文中对其进行评估?

I can send the object to a helper function and could mangle my own way through parsing it to return a string, but I can't figure out a way to get handlebars to essentially treat it as a partial or expand the variable to then evaluate it.

我可以将对象发送到辅助函数并且可以通过解析它来返回字符串来破坏我自己的方式,但是我无法找到一种方法来获得把手基本上将其视为部分或扩展变量然后评估它。

1 个解决方案

#1


0  

I don't think what you are trying to do is possible, and I don't think it would be a good practice if it were.

我不认为你想做的事情是可能的,如果是的话,我认为这不是一个好的做法。

Though I'm not sure I see the difference versus a function that would create your string and concatenate your variables outside the template.

虽然我不确定我是否会看到与创建字符串并将变量连接到模板之外的函数的区别。

For example, in my Backbone/Marionette App, I'd have

例如,在我的Backbone / Marionette应用程序中,我有

  templateHelpers: function() {
    return {
      tooltipString: "Blah, blah " + this.e1 + " " + this.f1 + " blah blah"
    };
  },

Another approach would be to move the string to a partial template if you really don't want that code in this template.

如果您真的不希望在此模板中使用该代码,则另一种方法是将字符串移动到部分模板。

// Partial template
<td>Blah, blah {{ e1 }} {{ f1 }}) blah blah<td>

And calling it as such

并称之为

{{#each json}}
  <td>{{> myPartial tooltip}}</td>
{{/each}}

If you want, you could have a variable partial template name in your object that you would use to call the right partial template.

如果需要,您可以在对象中使用可变的部分模板名称来调用正确的部分模板。

{{> (lookup . 'partialTemplateVariable') }}

#1


0  

I don't think what you are trying to do is possible, and I don't think it would be a good practice if it were.

我不认为你想做的事情是可能的,如果是的话,我认为这不是一个好的做法。

Though I'm not sure I see the difference versus a function that would create your string and concatenate your variables outside the template.

虽然我不确定我是否会看到与创建字符串并将变量连接到模板之外的函数的区别。

For example, in my Backbone/Marionette App, I'd have

例如,在我的Backbone / Marionette应用程序中,我有

  templateHelpers: function() {
    return {
      tooltipString: "Blah, blah " + this.e1 + " " + this.f1 + " blah blah"
    };
  },

Another approach would be to move the string to a partial template if you really don't want that code in this template.

如果您真的不希望在此模板中使用该代码,则另一种方法是将字符串移动到部分模板。

// Partial template
<td>Blah, blah {{ e1 }} {{ f1 }}) blah blah<td>

And calling it as such

并称之为

{{#each json}}
  <td>{{> myPartial tooltip}}</td>
{{/each}}

If you want, you could have a variable partial template name in your object that you would use to call the right partial template.

如果需要,您可以在对象中使用可变的部分模板名称来调用正确的部分模板。

{{> (lookup . 'partialTemplateVariable') }}