a Sensio Labs Product

The flexible, fast, and secure
template engine for PHP

Filters » default

Table of Contents

Questions & Feedback

License

Creative Commons License Twig documentation is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.

default

The default filter returns the passed default value if the value is undefined or empty, otherwise the value of the variable:

1
2
3
4
5
6
7
{{ var|default('var is not defined') }}

{{ var.foo|default('foo item on var is not defined') }}

{{ var['foo']|default('foo item on var is not defined') }}

{{ ''|default('passed var is empty')  }}

When using the default filter on an expression that uses variables in some method calls, be sure to use the default filter whenever a variable can be undefined:

1
{{ var.method(foo|default('foo'))|default('foo') }}

Note

Read the documentation for the defined and empty tests to learn more about their semantics.

Arguments

  • default: The default value
This website is powered by PHP and Twig. The Twig logo is © 2010-2012 Sensio Labs