简体   繁体   English

车把:自定义 for 循环助手中的部分变量

[英]handlebars: partial variable inside custom for loop helper

I found a very useful for loop from this SO answer我从这个 SO 答案中发现了一个非常有用的循环

It works well, but my code seems to lose context to the variable being passed to the partial when I use it.它运行良好,但是当我使用它时,我的代码似乎失去了传递给局部变量的上下文。 Is there a way that I can pass the "assessmentType" variable so that it continues on inside of this helper?有没有办法可以传递“assessmentType”变量,以便它在这个助手内部继续? I have the exact thing working just fine outside of this loop but it seems to lose context completely inside of it.我有确切的东西在这个循环之外工作得很好,但它似乎完全失去了它里面的上下文。 The context of "this" now belongs to the index number that the for loop is returning so I can't use that. “this”的上下文现在属于 for 循环返回的索引号,所以我不能使用它。

 // for loop helper hbs.registerHelper('for', (from, to, block) => { var accum = ''; for (var i = from; i < to; i++) { accum += block.fn(i); } return accum; }); // helper that takes in the assessment type // and returns a path to a png file that I have hbs.registerHelper("assessment_path", (assessmentType) => { if (assessmentType === "Leadership") return "leadership/Leadership"; if (assessmentType === "Human") return "human/Human" })
 {{.-- partial --}} {{> intro assessmentType="Leadership"}} {{!-- where the variable is being used--}} {{#for 0 20 }} <div class="page"> <img src="/img/{{{assessment_path assessmentType}}}_intro_{{this}}.png"> </div> {{/for}}

I was able to do this by using the "../" to get the previous context, I've previously tried this by using the @ sign as I've done before but that didn't work.我可以通过使用“../”来获取先前的上下文来做到这一点,我之前已经尝试过使用 @ 符号,就像我之前所做的那样,但这没有用。 Solution code解决方案代码

 {{#for 0 20 }} <div class="page"> <img src="/img/{{{assessment_path../assessmentType}}}_intro_{{this}}.png"> </div> {{/for}}

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

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