Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass variables to handlebars helper call

I'd like to pass template data to a "textfield" helper method I have defined, like this:

{{textfield label="{{label}}"
            id="account_{{attributes.id}}"
            name="account[{{attributes.name}}]"
            class="some-class"
            required="true"}}

(note the {{label}} and {{attributes.id}} references inside the {{textfield}} helper call)

Here is where I set up the template:

data = {
  "attributes": {
    "id": "name",
    "name": "name"
  },
  "label": "Name"
}
var templateHtml = 'markup here';
var template = Handlebars.compile(templateHtml);
var formHtml = template(data);

Here is a jsFiddle.

When I run this, I still see {{placeholders}} in the compiled markup.

How can I accomplish this?

like image 929
Chad Johnson Avatar asked Feb 15 '23 14:02

Chad Johnson


1 Answers

You're using the incorrect syntax to pass named parameters to your handlebars helper. What you want is something like this:

var data = {
  "attributes": {
    "name": "name"
  }
}
var templateHtml = '{{textfield name=attributes.name}}';
var template = Handlebars.compile(templateHtml);
var formHtml = template(data);

And an updated fiddle: http://jsfiddle.net/3yWn9/1/

like image 135
pifantastic Avatar answered Feb 18 '23 11:02

pifantastic