{# 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 '/\{/' -%}
        {# The title comes from page frontmatter (page.header.flex.collection.title)
           and is editor-controlled. Render it through evaluate_twig (the sandboxed
           Twig::processString path) instead of template_from_string, which would
           produce an unsandboxed sub-template and turn an editor-supplied title
           into arbitrary server-side Twig. GHSA-623v-m3c4-3pw8. #}
        {%- set title = evaluate_twig(title)|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 %}
