Jinja has a built in default filter that can be used to set a default value when one isn’t provided.


default(value, default_value=u’’, boolean=False)


If the value isn’t defined, default will display what’s in the default_value field. Passing in a true as the second parameter will return the default value if the value is defined but blank.

One of the most common use cases is to use the default value for a customer’s name:

  Hey {{ first_name|default("there", true) }},

Remember that Jinja can can be used in any part of the template: HTML, CSS, Plain Text, Subject Line, Preheader. This can be useful for setting default styles in a snippet.

  body {
      background-color: {{ background_color|default("#FFFFFF", true) }}
  }

It’s also useful for setting a default loop control:

  {% if loop.index0 < total_items|default(5, true) %}