简体   繁体   中英

Handlebars template with subexpresion

I am having trouble trying to get a subexpression in handlebars. I have this object

{
  vehicles:{
    air:"airplane",
    water:"boat",
    land:"cars\nvans",
  }
}

I have this helper

Handlebars.registerHelper('split', function(str, ch) {
    if (!str) return '';
    if (typeof ch !== 'string') ch = /\n/;
    return str.split(ch).map(function(splitted) {
    splitted.trim();
});
});

I would like to have an output like this

air/airplane
water/boat
land/cars
land/vans

With this template

{{#each vehicles}}
    {{#each (split this)}}
        {{../key}}/{{{this}}}
    {{/each}}
{{/each}}

I debugged and the split helpers get the values correctly, i think it has to be something with the scope of my this

Thank you very much

很好地发现了我的问题,在嵌套的#each访问对象的key的方法不是../@key ,事实证明它应该是@../key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM