繁体   English   中英

如何引用当前块变量之外的变量?

[英]How to reference a variable outside the current block variable?

假设我在传递给我的模板的结构中有两个数组:

{
  days: 'Mon,Tue,Wed,Thu,Fri'.split(','),
  times: 'Morning,Afternoon,Night'.split(',')
}

我想列出每一天,每天都列出每一天。 但是,当我在days数组中引用循环内的times数组时,似乎我认为我想要当前days元素的成员。 如何从循环内引用一个成员而不是另一个成员?

 var theTemplate = Handlebars.compile(document.getElementById('example').innerHTML); var data = { days: 'Mon,Tue,Wed,Thu,Fri'.split(','), times: 'Morning,Afternoon,Night'.split(',') }; document.body.innerHTML += theTemplate(data); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script> <script id="example" type="text/x-handlebars-template"> <ul> {{#days}} <li> {{.}} <ul> {{#times}} <li>{{.}}</li> {{/times}} </ul> </li> {{/days}} </ul> </script> 

您应该使用父路径段( ../ ),它引用父模板范围:

 var theTemplate = Handlebars.compile(document.getElementById('example').innerHTML); var data = { days: 'Mon,Tue,Wed,Thu,Fri'.split(','), times: 'Morning,Afternoon,Night'.split(',') }; document.body.innerHTML += theTemplate(data); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script> <script id="example" type="text/x-handlebars-template"> <ul> {{#days}} <li> {{.}} <ul> {{#../times}} <li>{{.}}</li> {{/../times}} </ul> </li> {{/days}} </ul> </script> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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