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>
pangratz
  • 15,875
  • 7
  • 50
  • 75
Kapil Garg
  • 3,100
  • 5
  • 19
  • 19

2 Answers2

40

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>
D.Shawley
  • 58,213
  • 10
  • 98
  • 113
  • 1
    Also, if you're using real html comments and want to include variables, make sure to use the [unbound helper](http://docs.emberjs.com/#doc=Handlebars.helpers&method=.unbound&src=false) `` – Ilia Choly Jun 01 '12 at 13:23
  • 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}}? – jacobq Sep 18 '12 at 13:30
  • 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 console – Epirocks Jan 17 '23 at 11:38
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)

Iftah
  • 9,512
  • 2
  • 33
  • 45
  • 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 :) ) – Richard Otvos Nov 12 '16 at 20:17
  • This was probably added in later versions? As the accepted answer is from 2012 and this is from 2015 – user2831723 Feb 16 '18 at 09:18