javascript - Nested top level each in handlebars -
given context
{ letters: ['a', 'b', 'c'], numbers: ['1', '2', '3'] }
how produce
a1 a2 a3 b1 b2 b3 c1 c2 c3
in essence,
{{#each letters}} {{#each ../numbers}} put here? {{/each}} {{/each}}
i using https://github.com/sunng87/handlebars-rust, in theory has close feature parity, how done in handlebars.js should compatible.
for reference, actual usecase c++ code generation. , question largely accessing this
propery of internal loop. right code looks like
{{#each variant}} {{#each ../typearg}}{{#if @first}}template <{{/if}}{{this}}{{#if @last}}>{{else}},{{/if}}{{/each}} {{/each}}
however reason, outputs
template <> template <>
when expecting output
template <t> template <t>
the object relevant , being passed in form
{ typearg: [ 't' ], variant: [{ }, { }] }
i think best way out create new handlebar helper return html
.
handlebars.registerhelper('multi_list', function(context, options) { var html = ""; var letters = options.hash.letters; var numbers = options.hash.numbers; (var i=0; i< letters.length; i++) { (var j=0; j < numbers.length; j++) { html += '<li>' + (letters[i] + '' + numbers[i] + '</li>'; } } return html; });
and call helper
{{#multi_list letters numbers}}
if want still iterate using existing each
helper, can use {{this}}
or {{./this}}
access innermost loop value ,
{{../this}}
access outermost loop value 1 level higher
{{#each variant}} {{#each ../typearg}} {{#if @first}} template < {{/if}} {{this}} {{#if @last}} > {{else}} , {{/if}} {{/each}} {{../this}} {{/each}}
Comments
Post a Comment