softmoth / raku-Template-Mustache

Raku library for the Mustache template format
https://modules.raku.org/dist/Template::Mustache:cpan:SOFTMOTH
Artistic License 2.0
21 stars 19 forks source link

Doubt about Mustache specification #36

Closed antoniogamiz closed 4 years ago

antoniogamiz commented 4 years ago

Hi, right now unused variables are replaced by an empty string:

use Template::Mustache;
Template::Mustache.render('{{bar}} {{foo}}', {foo=>"d"}) # will print ' d'

Is there a way to disable this? Or is not possible? It would be extremely helpful to concatenate renders without replacing all variables at the same time.

antoniogamiz commented 4 years ago

I thought lambdas could work but they cannot...

use Template::Mustache;

say Template::Mustache.render(
  '{{#wrapped}} {{name}} is awesome. {{/wrapped}}'
, {wrapped => -> $text { $text }});
softmoth commented 4 years ago

It sounds like you would want to use Inheritance, with several levels. For example, this spec test.

It would be possible to make a pragma to leave variables in place if they are not defined in the context. That's definitely not how mustache is designed, but it's a sensible and easy feature to add.

I'd rather use the Inheritance feature though, if I'm right that it would be a better fit for what you're ultimately trying to do.

antoniogamiz commented 4 years ago

I do not see how Inheritance with several levels can solve this. What I want is to generate the menu only once, to avoid replacing it with each HTML file generated. At this moment, I have implemented this behavior with two renders:

The problem is that the first render replaces all unused variables by an empty string, so the second render does nothing. I think if I use partials or inheritance, all menu data will have to be replaced with each HTML file, so it will take the same time. With these two renders, I reduce the generation time around 400 seconds.

You can see the changes in this commit: https://github.com/Raku/Documentable/commit/b683b7a9cf21c060988c67a50e8bc52179374118.

Now my question is, can I get that behavior with Inheritance?

softmoth commented 4 years ago

I didn't get to trying your scenario with inheritance, but I did implement the pragma. It's not merged to master yet, but you can try it out in the keep-undef-pragma branch.

antoniogamiz commented 4 years ago

Oh, that was fast. Thanks a lot, @softmoth, it's a great help to have this feature :).

softmoth commented 4 years ago

I've committed this to master and released it to CPAN as version 1.2.2.