{{#if foo}} {{foo}} {{/ if}} is there a better way to check the value in handlebars.js? (Quiet reference notation)

Is there a better way?

{{#if firstName }}  {{ firstName}}  {{/if}}
{{#if middleName }} {{ middleName}} {{/if}}
{{#if lastName }}   {{ lastName}}   {{/if}}

I often do not have data.

+5
source share
2 answers

With handles, if the value is null or empty descriptors, spit out the empty string that Nick Kitto mentioned.

so just {{firstName}} {{middleName}} {{lastName}} will be fine. However, if the middle name was blank, you should have had 2 spaces between the first and last name, as for the only possible problem that I see.

+1
source

You can also use {{#with firstName}} {{this}} {{/with}}

The best way is to save the name as an object {name:{first:'James', last:'Bond'}}, and then:

{{#with name}}{{last}}, {{first}} {{last}}{{/with}}

+1
source

All Articles