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?
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.
nil is not empty, it is a nil value.
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.
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.
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}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With