Flex

TIP: Complete list of methods can be found from Customizing Flex Objects section.

count()

count(): int Count the number of directories registered to Flex.

Returns:

{% set flex = grav.get('flex') %}

Flex has {{ flex.count() }} enabled directories.

getDirectories()

getDirectories( [names] ): array Get list of directories.

Parameters:

  • names Optional: List of directory names (array)

Returns:

TIP: If no list of names was provided, method returns all directories registered to Flex.

{% set flex = grav.get('flex') %}

{# Get all directories #}
{% set directories = flex.directories() %}

{# Get listed directories #}
{% set listed_directories = flex.directories(['contacts', 'phonebook']) %}

{# Do something with the directories #}

TIP: You may want to make sure you return only the directories you want to.

hasDirectory()

hasDirectory( name ): bool: Check if directory exists.

Parameters:

  • name Name of the directory (string)

Returns:

  • bool True if found, false otherwise
{% set flex = grav.get('flex') %}

Flex has {{ not flex.hasDirectory('contacts') ? 'not' }} contacts directory.

getDirectory()

getDirectory( name ): Directory | null Get a directory, returns null if it was not found.

Parameters:

  • name Name of the directory (string)

Returns:

  • Directory (object)
  • null Directory not found
{% set flex = grav.get('flex') %}

{# Get contacts directory (null if not found) #}
{% set directory = flex.directory('contacts') %}

{# Do something with the contacts directory #}

Check what you can do with Flex Directory

getObject()

getObject( id, directory ): Object | null Get an object, returns null if it was not found.

Parameters:

  • id ID of the object (string)
  • directory Name of the directory (string)

Returns:

  • Object (object)
  • null Object not found
{% set flex = grav.get('flex') %}

{% set contact = flex.object('ki2ts4cbivggmtlj', 'contacts') %}

{# Do something #}
{% if contact %}
  {# Got Bruce Day #}
  {{ contact.first_name|e }} {{ contact.last_name|e }} has a website: {{ contact.website|e }}
{% else %}
  Oops, contact has been removed!
{% endif %}

Check what you can do with Flex Object

getCollection()

getCollection( directory ): Collection | null Get collection, returns null if it was not found.

Parameters:

  • directory Name of the directory (string)

Returns:

{% set flex = grav.get('flex') %}

{% set contacts = flex.collection('contacts') %}

{# Do something #}
<h2>Ten random contacts:</h2>
<ul>
  {% for contact in contacts.filterBy({published: true}).shuffle().limit(0, 10) %}
    <li>{{ contact.first_name|e }} {{ contact.last_name|e }}</li>
  {% endfor %}
</ul>

Check what you can do with Flex Collection

Found errors? Think you can improve this documentation? Simply click the Edit link at the top of the page, and then the icon on Github to make your changes.

Results