Twig :: select

The select filter allows you to pick out values from an array of associative arrays or built-in stakx objects such as ContentItems and DataItems.

Since

0.1.1

Changelog

  • 0.1.2 - The ignore_null argument was added

Usage

select(key, flatten = true, distinct = true, ignore_null = true)

Parameters

  • (required) key : string

    The associative key that we'll be picking values from.

  • flatten : bool

    Whether or not to flatten multi-dimensional arrays into a single array.

  • distinct : bool

    Whether or not to remove duplicate items from the flattened array. This is only available when flatten is set to true.

  • ignore_null : bool

    Whether or not to include null values in the returned array.

Example Usage

From an array of objects, each with a username value, I can use the select filter to pull the unique values in each of those fields.

{% set object_array = [{ 'username': 'allejo' }, { 'username': 'immortalwombat' }] %}

<!-- ['allejo', 'immortalwombat'] -->
{{ object_array | select('username') }}

Or if I would like to get all of the distinct tags used in all of my posts that I've written in a blog.

{{ collections.posts | select('tags') }}