2

I have a function in my Symfony controller which returns the following: test+testString1+test2

TestController.php

public function getResultAction($fileName) {
    $string1="test";
    $string2="testString1";
    $string3="test2";
    $response = $string1."+".$string2."+".$string3;

    return new Response($response);
}

In my twig I've rendered the function in my controller:

test.html.twig

{% set test %}
    {{ render(controller('TestBundle:Test:getResult')) }}|split('+', 4)
{% endset %}

{{ test[0] }}

I'm using twig split filter so that I can display test, testString1 and test2 individually. But then whenever I attempt to display test[0], I receive the following error:

Impossible to access a key "0" on an object of class "Twig_Markup" that does not implement ArrayAccess interface in TestBundle:Test:test.html.twig

What's wrong with what I'm doing? I hope you could help me with this. Thanks

1 Answer 1

1

You miss the split filter inside the double brace as follow:

{% set test %}
    {{ render(controller('TestBundle:Test:getResult')) |split('+', 4) }}
{% endset %}

Hovenever seems same problem with the set tag. From the doc:

The set tag can also be used to 'capture' chunks of text

Try with:

{% 
   set test=render(controller('TestBundle:Test:getResult'))|split('+', 4)
%}

Hope this help

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

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.