2

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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"] %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.