canjs / can-stache

Live binding handlebars templates
https://canjs.com/doc/can-stache.html
MIT License
10 stars 13 forks source link

Returning a fragment from a section helper doesn't work #647

Open phillipskevin opened 5 years ago

phillipskevin commented 5 years ago

In 3.0, this would create a fragment containing <p>this is foo</p>:

stache.addHelper("foo", (options) => {
  return stache("<p>this is foo</p>")();
});

const frag = stache(`
    {{# foo() }}
      <p>this is NOT foo</p>
    {{/ foo }}
`)();

document.body.appendChild(frag);

...in 4.0, it only contains <p>this is NOT foo</p>.

3.0 version: https://codepen.io/kphillips86/pen/eQwjZz 4.0 version: https://codepen.io/kphillips86/pen/EOBpVZ

cc @mjstahl.

justinbmeyer commented 5 years ago

I'm not sure this is necessarily wrong ... foo() is returning a truthy value, so it makes sense that <p>this is NOT foo</p> would then be readered as if foo() returned a true or 1.

We could change this to make # VALUE do something different if given a fragment.

phillipskevin commented 5 years ago

Yeah, I can’t think of a situation where you would want to return a fragment and not have it rendered.

Alternatively, you can make this work by setting options.metadata.rendered = true; in the helper. This is what stache does internally when you call options.fn() or options.inverse() so it knows to display the fragment.