22
votes

I'm learning ember.js and would like to sometimes deactivate some chunks of code. I know {{! }} works for single-line commenting inside <script type="text/x-handlebars">, but I can't make it work for multi-line commenting. Maybe because I have conditional statements inside.

<script type="text/x-handlebars" id="stuff">
    {{!
      {{#if length}}
        foobar
      {{/if}}
    }}
</script>

but then I got this error:

Uncaught Error: Parse error on line xx:
...ngth}}    foobar  {{/if}}}}    {{ o
---------------------^
Expecting 'EOF', got 'OPEN_ENDBLOCK' 

I also tried using <!-- ... -->, while the section is not shown, but I also get this error:

Uncaught Error: Cannot perform operations on a Metamorph that is not in the DOM

this error doesn't show up if I just delete that chunk of code.

4
You will have to add an exclamation mark to any handlebars statements {{! ... }} inside of the <!-- --> comment.CraigTeegarden

4 Answers

55
votes

Add

{{!--
  This is a
  multiline
  comment
--}}

for multiline comments

7
votes

Any comments that must contain }} or other handlebars tokens should use the {{!-- --}} syntax.

source: http://handlebarsjs.com/

So it looks like {{! }} is valid for multiline as long as there are no other handlebars tokens inside it. Otherwise, {{!-- --}} is necessary.

0
votes

In visual studio code, I used to select the part of the code and do ctrl + / to comment or to remove content. It will add
{{!-- Any hbs code or anything will be wrapped --}}

-2
votes

{{! }} seems to be working. Take a look here.