0

I found a very useful for loop from this SO answer

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? 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.

// 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}}
LTFoReal
  • 187
  • 2
  • 11

1 Answers1

0

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}}
LTFoReal
  • 187
  • 2
  • 11