Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

{{#if foo }}{{ foo}}{{/if}} is there a better way to check for 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 have missing data.

like image 318
ToddBallard Avatar asked Sep 18 '12 21:09

ToddBallard


People also ask

What are {{ }} in HTML?

The {{ ___________ }} syntax is for template variables. This is used to interpolate a variable declared either as one of the built-in options or your own which you have created from any number of methods (models, view context, etc).

What does {{ name }} mean in Django templates?

8. What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

What {% include %} does?

{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.

Does Django use Jinja2?

The default syntax of Jinja2 matches Django syntax in many ways. However this similarity doesn't mean that you can use a Django template unmodified in Jinja2. For example filter arguments use a function call syntax rather than a colon to separate filter name and arguments.


2 Answers

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

Best way would be to save the name as an object {name:{first:'James', last:'Bond'}} and then:

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

like image 83
Ronen Teva Avatar answered Nov 08 '22 20:11

Ronen Teva


With handlebars if the value is null or empty handlebars will spit out an empty string as Nick Kitto mentioned.

so just {{firstName}} {{middleName}} {{lastName}} would be fine. However, if the middle name were blank you'd end up with 2 spaces between the first and last name, that's about the only possible problem I can see.

like image 1
Chris M Avatar answered Nov 08 '22 22:11

Chris M