How can i get a easy and clear way that set the first radio button is checked in Handlebars template. tks
template:
<form>
{{#each this}}
<input value="{{value}}" />
{{/each}}
</form>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
expect render:
<form>
<input value="val 1" checked />
<input value="val 2" />
<input value="val 3" />
</form>
thanks all.
{{#each}}in Handlebars doesn’t give you access to the iteration number or anything like that so you can’t do it without altering your template and data a little bit:and then add
selvalues to your data:Demo: http://jsfiddle.net/ambiguous/27Ywu/
You could of course just set
sel: trueon the first element of your data array:Demo: http://jsfiddle.net/ambiguous/yA5WL/
Alternatively, use jQuery to check the first one after you have the HTML:
Demo: http://jsfiddle.net/ambiguous/sPV9D/
Newer versions of Handlebars do give you access to the index:
So, if you’re using the latest Handlebars, you can do something special by using the fact that:
@indexwill be zero.That lets you do this:
Demo: http://jsfiddle.net/ambiguous/PHKps/1/
Of course, picking out any other index is harder and leaves you either modifying the input data (as before) or adding some sort of
{{#if_eq}}custom helper.