XaminProject / handlebars.php

Handlebars processor for php
331 stars 134 forks source link

Commenting out expressions doesn't work as expected #65

Open jslegers opened 10 years ago

jslegers commented 10 years ago

It's unclear to me whether it's possible to comment out expressions and have handlebars.php completely ignore anything in a comment, which is possible in pretty much every programming language and commonly used for temporarily storing deleted code that may be fully or partially restored at a later time.

Currently, handlebars.php tries to parse any {{ }} it can find as an expression, including those within comments. This results in a fatal error when no helper can be found with that name.

That means I can't do this :

<!--
<html>
    <body>
        Hello {{name}}</br>
        You have just won ${{value }}!</br>
        {{#in_ca}}
        Well, ${{taxed_value}}, after taxes.</br>
        {{/in_ca}}
    </body>
</html>
-->

This also doesn't work :

{{!-- comment}}
<html>
    <body>Hello {{name}}</br>
        You have just won ${{value}}!</br>
        {{#in_ca}}
        Well, ${{taxed_value}}, after taxes.</br>
        {{/in_ca}}
    </body>
</html>
{{comment --}}

This doesn't work either :

{{!--
<html>
    <body>Hello {{name}}</br>
    You have just won ${{value}}!</br>
    {{#in_ca}}
    Well, ${{taxed_value}}, after taxes.</br>
    {{/in_ca}}
    </body>
</html>
--}}

Neither does this :

{{!-- <html><body>Hello {{name}}</br>You have just won ${{value}}!</br>{{#in_ca}}</br>Well, ${{taxed_value}}, after taxes.{{/in_ca}}</body></html> --}}

Whatever I try, I keep getting the following error :

<b>Fatal error</b>:  Uncaught exception 'RuntimeException' with message 'in_ca is not registered as a helper' in /var/www/monitoring/Vendor/Handlebars/Template.php:349
Stack trace:
#0 /var/www/monitoring/Vendor/Handlebars/Template.php(404): Handlebars\Template-&gt;_mustacheStyleSection(Object(Handlebars\Context), Array)
#1 /var/www/monitoring/Vendor/Handlebars/Template.php(162): Handlebars\Template-&gt;_section(Object(Handlebars\Context), Array)
#2 /var/www/monitoring/Vendor/Handlebars/Handlebars.php(173): Handlebars\Template-&gt;render(Object(Application\View_Model))
#3 /var/www/monitoring/dashboard.php(46): Handlebars\Handlebars-&gt;render('&lt;!DOCTYPE html&gt;...', Object(Application\View_Model))
#4 {main}
  thrown in <b>/var/www/monitoring/Vendor/Handlebars/Template.php</b> on line <b>349</b><br />

This isn't how I expect comments to work.

DavideMontersino commented 9 years ago

I think that {{!-- }} comments are not supported at all; only {{! expression }} style comments are supported.

I think this should be implemented in src/Handlebars/Tokenizer.php.. if I have time to study the library I'll fix that!

JustBlackBird commented 9 years ago

@jslegers the original comments syntax is {{! This is a comment }}. Moreover a comment cannot contain closing stashes (}}).

In your last example anything after the first }} token is treated as a template, not as a comment. I think that you have not registered in_ca helper, that's why you've got an exception.

I believe that Handlebars.php behavior is correct and correspond to Handlebars.js behavior.

primehalo commented 8 years ago

Handlebars.js allows comments that contain handlebar tokens with the {{!-- --}} syntax, and looks like that functionality has been there since at least 2013. Has this still not been implemented in Handlebars.php?

jcubic commented 7 years ago

+1 for comments.