Skip to main content

Utility Filters

General-purpose utility filters.

Default Value

FilterArgumentsDescription
defaultvalueReturn fallback if empty

Returns the fallback value if the input is empty (''), null, false, or an empty array.

{{ post.subtitle|default('No subtitle') }}
{{ user.bio|default('No biography available.') }}
{{ item.price|default(0) }}
{{ settings.color|default(theme.primary_color) }}

Note: The default filter treats false and 0 differently:

  • false → returns default
  • 0 → returns 0 (not empty)

JSON Encoding

FilterArgumentsDescription
json_encodeoptionsEncode value as JSON

Arguments:

  • options — JSON encoding options (default: 0)
{{ data|json_encode }}
{{ settings|json_encode }}

Use in data attributes

<div data-config='{{ settings|json_encode }}'>

Pass data to JavaScript

<script>
var config = {{ settings|json_encode }};
</script>

Fallback Operators

In addition to filters, UnBlock supports fallback operators for handling missing or empty values.

Null Coalescing (??)

Returns the fallback if the value is null or undefined.

{{ post.subtitle ?? 'No subtitle' }}
{{ post.image ?? post.thumbnail ?? '/default.jpg' }}

Behavior: Preserves false, 0, '', and [].

Elvis Operator (?:)

Returns the fallback if the value is falsy.

{{ post.views ?: 0 }}
{{ post.subtitle ?: post.title }}

Behavior: Returns fallback for null, false, 0, '', and [].

Comparison

Value?? returns?: returns|default returns
'text''text''text''text'
nullfallbackfallbackfallback
falsefalsefallbackfallback
00fallback0
''''fallbackfallback
[][]fallbackfallback