Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8529167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:58:12+00:00 2026-06-11T08:58:12+00:00

I can`t pass an array from a symfony 2 controller to a TWIG template.

  • 0

I can`t pass an array from a symfony 2 controller to a TWIG template. I use this code in the controller:

$searchTerms['color'] = "Red";
return $this->render('TestBundle::search.html.twig', 
        array(
            "searchTerms" => $searchTerms));

In the twig template, I am trying to access the variable like this:

  1. {{ searchTerms[‘color’] }}
  2. {{ searchTerms.color }}

Both output nothing, empty string, so it seems like array comes to template but its elements are empty.

What`s wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:58:13+00:00Added an answer on June 11, 2026 at 8:58 am

    Yes, this code works. The first thing to check is that your twig code is in the correct page (TestBundle::search.html.twig). This might sound silly but that happens sometimes…

    If this is all good, I suggest that you try to debug within your template. Debugging is the most important thing. You will always have this kind of problem while programming, especially when you try something new. The better you are at debugging your code, the better you are as a programmer because there is no way you can get everything right the first time.

    So, how can you debug?

    To debug within your twig template, you can use the debug extension of twig. To activate the debug option, you will have to do a quick change in your config file. You can also read this thread if your lost.

    You can debug any variable within your template like this:

    <pre>
    {% debug searchTerms %}
    </pre>
    

    This way, you can easily debug your variable and test what your problem is:

    {% debug searchTerms['color'] %}
    

    If you want to debug things quickly, I highly recommend that you use the LadyBugBundle. It is an awesome tool that will allow you to do something like that:

    In your controller:

    ladybug_dump($searchTerms);
    

    In your TWIG template:

    {{ searchTerms|ladybug_dump }}
    

    Not that different from a classic var_dump option, but if you have long arrays or objects, ladybug will impress you. More importantly, in a controller, you will often have the need to stop the code at a certain point to avoid the page to load after your debug statement, this is fairly easy with ladybug:

    ladybug_dump_die($searchTerms);
    

    You can even ask ladybug to load the “debugged” variable into Symfony profiler with this simple statement.

    $this->get('ladybug')->log($searchTerms);
    

    You have now direct access of the variable from a tab of the Symfony2 profiler.
    Ladybug can do a lot more, but for this, the doc is really good.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have CodeIgniter question. How can I pass an array from controller to view?
From a method, I can pass a struct which contains an array of integers,
I have an array which contains around 50-200 hyperlinks. How can I pass this
I want to pass a list array from the View to the controller on
How can I save to my mysql db an array from flash? I pass
How i can pass Array List from one Activity to another my array list
I want to pass array and return array from a procedure, the following is
i just want to know can we pass nsdata or array datatype from one
How can I pass an array of integer from a button to another one?
I need to pass an array from C#.net application to oracle stored procedure. Can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.