{# flex-objects/views/directory.html.twig #}

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

{% if directory %}
    {% set layout = options.layout ?? 'default' %}
    {% set context = options ?? {} %}
    {% set type = directory.flexType() %}
    {% set collection = directory.collection %}

    {%- set title = options.title -%}
    {%- if title matches '/\{/' -%}
        {%- set title = include(template_from_string(title, 'collection title template'))|trim -%}
    {%- endif -%}
    {% if title %}
        {% do page.title(title) %}
    {% endif %}

    {% render collection layout: layout with context|merge({title: title}) %}

{% else %}
    {% include 'flex-objects/views/404.html.twig' %}
{% endif %}
