Abdullah Seba
Abdullah Seba

Reputation: 344

How to include twig file from array?

Hi how do I pass a array to a twig include?

{% set navbar_logo %}["{{sprinkle|raw}}/components/content/navbar/navbar-logo.html.twig", "/components/content/navbar/navbar-logo.html.twig"]{% endset %}

{% include navbar_logo %}

this results in:

Unable to find template "["@admin/components/content/navbar/navbar-logo.html.twig", "/components/content/navbar/navbar-logo.html.twig"]"

this works fine:

{% include ["{{sprinkle|raw}}/components/content/navbar/navbar-logo.html.twig", "/components/content/navbar/navbar-logo.html.twig"] %}

But i need to get it from the variable.

This also works:

 {% set navbar_logo %}{{sprinkle|raw}}/components/content/navbar/navbar-logo.html.twig{% endset %}

{% include navbar_logo %}

But I need the backup incase the first one does not exist. How can I do this?

Upvotes: 2

Views: 342

Answers (2)

Alain
Alain

Reputation: 36954

What about:

{% set navbar_logo = include(sprinkle|raw ~ '/components/content/navbar/navbar-logo.html.twig') %}

I don't exactly know what you're trying to achieve, but a better approach would be to set this in a block of your base layout.

{% block navbar_logo %}
    {{ include(sprinkle|raw ~ '/components/content/navbar/navbar-logo.html.twig') }}
{% endblock %}

Then when you later need to dump your logo, use {{ block('navbar_logo') }}.

More about blocks

Upvotes: 2

DarkBee
DarkBee

Reputation: 15634

If you're using {% set .. %}... {% endset %}, Twig is treating the variable as a string

You should switch your code to this and then it works

{% set navbar_logo = [ sprinkle~"/components/content/navbar/navbar-logo.html.twig", "/components/content/navbar/navbar-logo.html.twig"] %}

Upvotes: 1

Related Questions