Handlebars.js: how does partials gets invoked in a helper? I got: TypeError: fn is not a function

StackOverflow https://stackoverflow.com/questions/23068614

  •  03-07-2023
  •  | 
  •  

Pregunta

I am learning Handlebars. It still appears to be a mystery to me that how a partials gets invoked in a helper.

I read this tutorial: http://blog.teamtreehouse.com/handlebars-js-part-2-partials-and-helpers

From this example in the tutorial,

    Handlebars.registerHelper("stripes", function(array, even, odd, fn) {
      var buffer = "";
      for (var i = 0, j = array.length; i < j; i++) {
        var item = array[i];

        // we'll just put the appropriate stripe class name onto the item for now
        item.stripeClass = (i % 2 == 0 ? even : odd);

        // show the inside of the block
        buffer += fn(item);  <!-- this is where a partials gets invoked -->
      }

      // return the finished buffer
      return buffer;
    });

it appears the partial is added and applied by Handlebars. However, I used this same approach in Handablebars 1.3.0 and 2.0 Alpha-2, it seems no longer the case. I always got the error:

TypeError: fn is not a function
buffer += fn(item);

I did quite online search and found a number tutorials, but none of them shows how partials is hooked up with a helper in version 1.3.0 or later.

Can someone help me out?

Thanks a lot!

¿Fue útil?

Solución

OK. I believed I solved this problem. In v1.0 or later, Handlebars puts everything in a hash, not just fn. So, everything in the above post still is valid except these two lines:

Handlebars.registerHelper("stripes", function(array, even, odd, options)

...

buffer += options.fn(item);

Hope this helps someone else. Any confirmation is welcome. I feel bad that there is no direct example on this, at least I did not find it.

Regards.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top