Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars template - "tilde" in if statement

We have statement like:

{{~#if someCondition ~}} 
<div class="whyweneedtildehere"></div> 
{{~/if~}}

What is the difference between simple if statement and if statement with "~" in handlebars templates?

like image 498
vvahans Avatar asked Sep 26 '14 08:09

vvahans


2 Answers

It is called a tilde, which might help you google it further.

The Handlebars docs answers your question in detail.

Template whitespace may be omitted from either side of any mustache statement by adding a ~ character by the braces. When applied all whitespace on that side will be removed up to the first handlebars expression or non-whitespace character on that side.

like image 93
Hein Haraldson Berg Avatar answered Nov 09 '22 03:11

Hein Haraldson Berg


Here are some examples that might help you understand what ~ does.

In .js:

{
  hello: 'Hello, World!',
}

Example #1:

.hbs

<div>
  {{hello}}
</div>

.html

<div>
  Hello, World!
</div>

Example #2:

.hbs

<div>
  {{~hello}}
</div>

.html

<div>Hello, World!
</div>

Example #3:

.hbs

<div>
  {{~hello~}}
</div>

.html

<div>Hello, World!</div>

Basically, it's for removing whitespaces in the output HTML.

like image 10
cglotr Avatar answered Nov 09 '22 05:11

cglotr