0

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:

index.handlebars snippet:

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

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

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

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.

T Schmidt
  • 32
  • 5
  • Just so you are aware you have 3 opening brackets {{{ – phenxd Feb 10 '16 at 20:35
  • And why can't you use {{ tooltip.e1 }} and {{ tooltip.f1 }}? – phenxd Feb 10 '16 at 20:40
  • If you want to output all the object's properties, http://stackoverflow.com/questions/9058774/handlebars-mustache-is-there-a-built-in-way-to-loop-through-the-properties-of – phenxd Feb 10 '16 at 20:42
  • Sorry I had edited the template for clarity a bit and a couple syntax errors. I can definitely use tooltip.e1, but the actual template code is contained in the tooltip object and not the handlebars template. – T Schmidt Feb 10 '16 at 21:18

1 Answers1

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

  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') }}
phenxd
  • 689
  • 4
  • 17