If condition inside ember template

I am getting errors trying to add the following if condition inside Ember template (actually part of component)

<div>
					{{#if field.value == ''}}
					  <span style="font-weight:bold">{{field.value}}</span>
					{{else}}
					  <span style="font-weight:bold">EMPTY</span>
					{{/if}}
				</div>

It says Error: Parse error on line 7:

...             {{#if field.value == ''}}                                         <span
-----------------------^
Expecting 'CLOSE_RAW_BLOCK', 'CLOSE', 'CLOSE_UNESCAPED', 'OPEN_SEXPR', 'CLOSE_SEXPR', 'ID', 'OPEN_BLOCK_PARAMS', 'STRING', 'NUMBER', 'BOOLEAN...

{{if}} handlebar doesn’t make comparisons between values, only check if a property is true/has_value or false/undefined.

1 Like

Hmm…so how do I check for empty string value ("") inside my component template

You can create a helper http://guides.emberjs.com/v2.1.0/templates/writing-helpers/

You could try an addon - ember-truth-helpers

@jmurphyau, in that addon, wouldn’t be more intuitive a helper structured like {{name property/value1 operator property/value2}}? for example {{cp a '===' b}}

I do not think so. The way that ember-truth-helpers works today makes each helper roughly independent (and easy to reason about). Forcing all operations through a single helper (you suggested cp which I assume means “compare”) would make things harder to reason about.

Yep, cp was intended to mean compare. I thought this way would be more readable and intuitive. In this post for example, the OP’s first approach was to do exactly what I suggested.

An empty string is a falsy value for Handlebars. (source)

{{#if field.value}} would do the job.

This is what keeps me a way from Ember. I had the same issue and the work around was not feasible.

The solutions for the OP’s problem are fairly simple: creating a helper or using an addon should do the job. I don’t know what looks so difficult about it :confused: . And for your post, I haven’t read it all, but since it’s from more than a year ago, you should check the new features that Ember has. Specially if we take into account how quickly it has been evolving :wink:

1 Like

{{#if (eq selectedTuneType “custom”)}}

When possible, I believe the “correct” answer is to create a computed that can return the result of the comparison. This doesn’t work in things like {{#each}} blocks, and in those cases you’d need some sort of helper or the equality addon.