I am using Handlebar.js as my templating engine. Now I want to comment out some of the blocks in my handlebar templates. But then I realized that Handlebar doesn't ignore the expressions inside the Handlebar comment block. Any workaround for this?
The newest version of Handlebars has block comments support :
{{!-- {{commented expressions}} --}}
https://github.com/wycats/handlebars.js/commit/a927a9b0adc39660f0794b9b210c9db2f7ddecd9
Just add an exclamation mark after the opening brackets.
Normal Expression:
{{expressions}}
Commented Expression:
{{!expressions}}
{{!
and {{!--
forms is that only the latter allows embedded handlebars tags. As that will be easy to forget, especially in a long comment that is later revised, I would suggest to always use the longer comment form.
Use this way in your handlebar template file.
<div class="entry">
{{!-- only output author name if an author exists --}}
{{#if author}}
<h1>{{author.firstName}} {{author.lastName}}</h1>
{{/if}}
</div>
The comments will not be in the resulting output. If you'd like the comments to show up, then use HTML comments.
<div class="entry">
{{! This comment will not be in the output }}
<!-- This comment will be in the output -->
</div>
Two ways to comment on handlebar.js
single component:
{{!fixedTop=true}} --> comments the whole content inside the brackets
Multiple component:
{{!--fixedTop=true
alignment=true--}} --> comments the whole content until end with "--"
offical site said "You can use comments in your handlebars code just as you would in your code. Since there is generally some level of logic, this is a good practice.
The comments will not be in the resulting output. If you'd like the comments to show up just use HTML comments, and they will be output.
Any comments that must contain }} or other handlebars tokens should use the {{!-- --}} syntax."
Check the site https://handlebarsjs.com/guide/#evaluation-context
Use this code:
{{#data}}
<!-- enter comments here -->
<p>{{name}}</p>
{{/data}}
Success story sharing