How to do commenting in emberjs script handlebars?
Asked Answered
S

2

28

Can anybody tell me how to include commented code in emberjs handlebars templates?

  <script id="restaurantDetail" data-template-name='restaurantDetail' type="text/x-handlebars">
//Commented code goes here
</script>
Swampy answered 27/3, 2012 at 20:9 Comment(0)
C
41

From the looks of the github page, you want {{! comment text here}}:

Comments

You can add comments to your templates with the following syntax.

{{! This is a comment }}

You can also use real html comments if you want them to end up in the output.

<div>
    {{! This comment will not end up in the output }}
    <!-- This comment will show up in the output -->
</div>
Cannoneer answered 27/3, 2012 at 20:23 Comment(3)
Also, if you're using real html comments and want to include variables, make sure to use the unbound helper <!-- Item {{unbound itemId}} -->Calicle
Are comments required to be in their own {{ }} container or are they allowed to be inline, such as {{name !this is App.controller.content.name}}?Hubsher
I find if I put in real html comments the ember-cli complains about them as being "unexpected" and spits them out all in the consoleKeynote
P
27

I recommend using {{!-- comment here --}} because this comment syntax can contain new lines and also }} inside the comment, for example:

Bad comments:
    {{! badly commented {{if somecondition "red" "blue" }} }}  
    {{! badly multiline comments
        another line  }}  

Comment that works:
    {{!-- this is commented correctly {{if somecondition "red" "blue" }} --}}
    {{!-- correct multiline comments
        another line  --}}  

(I know this is an old question, but this answer appears first on Google when searching for ember template comments, so I wanted to help future readers)

Punish answered 4/8, 2015 at 14:59 Comment(2)
This is definitely the way to go, helped me a great deal while learning Ember ( just make sure you don't litter your templates with commented out code :) )Scission
This was probably added in later versions? As the accepted answer is from 2012 and this is from 2015Papp

© 2022 - 2024 — McMap. All rights reserved.