0

How to create and pass multiarray in Twig?

{{ include('box.html.twig', {multi: {aaa: bbb, ccc: ddd}}) }}

Is good but I would like have to many elements in variable multi:

{{ include('box.html.twig', {multi: {aaa: bbb, ccc: ddd}, {aaa: eee, ccc: fff}}) }}

It doesn't work.

{{ include('box.html.twig', {multi: {{aaa: bbb, ccc: ddd}, {aaa: eee, ccc: fff}}}) }}

and:

{{ include('box.html.twig', {multi: [{aaa: bbb, ccc: ddd}, {aaa: eee, ccc: fff}}]) }}

Also doesn't work.

1 Answer 1

1

Try like below:

{% include 'box.html.twig' with {'multi': {{'aaa': 'bbb', 'ccc': 'ddd'}, {'aaa': 'eee', 'ccc': 'fff'}}} %}

For more information check https://twig.sensiolabs.org/doc/2.x/tags/include.html

Note:if you are using without quotes php will consider as defined word.

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

4 Comments

In your example I have error: "A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{". ". I use Symfony 2.6.13.
In your version try with double Curly brackets
Can you give me example? I tried: {% include 'box.html.twig' with {"multi": {{"aaa": "bbb", "ccc": "ddd"}, {"aaa": "eee", "ccc": "fff"}}} %} and I have still error: "A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{". ".
Try with {{ instead of {%

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.