handlebars:自定义 for 循环助手中的部分变量
handlebars: partial variable inside custom for loop helper
我从这个 SO answer
中找到了一个非常有用的 for 循环
它运行良好,但我的代码似乎在我使用它时失去了传递给局部变量的上下文。有没有一种方法可以传递 "assessmentType" 变量,以便它在这个助手内部继续?我在这个循环之外工作得很好,但它似乎完全失去了它内部的上下文。 "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}}
我能够通过使用“../”来获取先前的上下文来做到这一点,我之前已经像以前那样通过使用@符号来尝试过,但是那没有用。解决方案代码
{{#for 0 20 }}
<div class="page">
<img src="/img/{{{assessment_path ../assessmentType}}}_intro_{{this}}.png">
</div>
{{/for}}
我从这个 SO answer
中找到了一个非常有用的 for 循环它运行良好,但我的代码似乎在我使用它时失去了传递给局部变量的上下文。有没有一种方法可以传递 "assessmentType" 变量,以便它在这个助手内部继续?我在这个循环之外工作得很好,但它似乎完全失去了它内部的上下文。 "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}}
我能够通过使用“../”来获取先前的上下文来做到这一点,我之前已经像以前那样通过使用@符号来尝试过,但是那没有用。解决方案代码
{{#for 0 20 }}
<div class="page">
<img src="/img/{{{assessment_path ../assessmentType}}}_intro_{{this}}.png">
</div>
{{/for}}