If condition inside ember template


#1

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...

#2

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


#3

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


#4

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


#5

You could try an addon - ember-truth-helpers


#6

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


#7

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.


#8

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.


#9

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

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


#10

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


#11

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: