HandlebarJS How to declare comments


In this tutorial, Learned about how to declare comments in handlebarJS template scripting language. Comments are useful text to describe about line or code component.

These will be ignore by compiler or interpreter in any programming language.

Does handlebarJS supports comments? Yes It supports and these are used to generate html content by handlebar engine.

Does it support inline block and multiline comments?

Inline comments are not supported, but support single and multi line comments

It supports two type of comments in HandlebarJS?

  • Comments shown in generated html output
  • Comments not shown in html output

Generated html not contains comments.

These comments are not visible in generated HTML output We can use either single or multi line comments

Single line comments

These are comments that are single line of text inside a double braces

Syntax

{{! comment text or expression}}

Example

{{! Check name exista and print }}

<div class="printname">
{{#if name}}
<h1>{{name}} {{salary}}</h1>
{{/if}}

Comment is not generated in html Generated HTML

<div class="printname">
<h1>john 5000</h1>

Multi line comments

These comments always starts with !– and comment text or expression can be spanned in multiple lines and ends with – inside a double braces

Syntax

{{!-- multi line 
comments 
--}}

Example

{{!-- Check if conditiona name
and print }} --}}
<div class="printname">
{{#if name}}
<h1>{{name}} {{salary}}</h1>
{{/if}}

Generated output does not contain comment text

<div class="printname">
<h1>john 5000</h1>

HTML comments inside handlebar template

These comments are generated in html and output as html comments.

These always wrapped inside a bracket syntax

Output

<div class="printname">
<!-- Checks Name is null and print name and salary -->
<h1>john 5000</h1>

Conclusion

Handlebar supports different comments single and multi line and html comments

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.