Is there a better way?
{{#if firstName }} {{ firstName}} {{/if}}
{{#if middleName }} {{ middleName}} {{/if}}
{{#if lastName }} {{ lastName}} {{/if}}
I often have missing data.
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).
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.
{% 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.
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.
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}}
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.
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