Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for empty-string, null or white-spaces in handlebar?

I need to check for null values, empty-strings and strings with whitespaces only and substitute them with "No Content".

I tried this but it worked only for null and empty string not for whitespaces-only strings.

{{#if value}}
    {{value}}
{{else}}
    <p>No Content</p>
{{/if}}

How to do it? Is helpers the only option?

like image 739
brainless Avatar asked Sep 29 '16 09:09

brainless


People also ask

What does an empty string look like?

An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null . It can be described as the absence of a string instance.

Is nil the same as empty string?

nil is not empty, it is a nil value.

Is null or whitespace Java?

We consider a string to be empty if it's either null or a string without any length. If a string only consists of whitespace, then we call it blank. For Java, whitespaces are characters, like spaces, tabs, and so on.

Is string null or empty Javascript?

Use the length property to check if a string is empty, e.g. if (str. length === 0) {} . If the string's length is equal to 0 , then it's empty, otherwise it isn't empty.


1 Answers

This is a useful Helper.

Handlebars.registerHelper('check', function(value, comparator) {
    return (value === comparator) ? 'No content' : value;
});

Tests template

{{#check value ""}}
  {{this}}
{{/check}}

{{#check value null}}
  {{this}}
{{/check}}

{{#check value undefined}}
  {{this}}
{{/check}}

like image 131
e-israel Avatar answered Oct 04 '22 15:10

e-israel