php 如何将数组从 Symfony 2 控制器传递到 TWIG 模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12356025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:24:53  来源:igfitidea点击:

How to pass an array from Symfony 2 controller to a TWIG template ?

phpsymfonytwig

提问by sphinks

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

我无法将数组从 symfony 2 控制器传递到 TWIG 模板。我在控制器中使用此代码:

$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 }}
  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?

怎么了?

回答by Mick

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...

是的,此代码有效。首先要检查的是您的树枝代码是否在正确的页面 ( TestBundle::search.html.twig) 中。这听起来可能很愚蠢,但有时会发生......

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 changein your config file. You can also read this threadif your lost.

要在您的 twig 模板中进行调试,您可以使用twig调试扩展。要激活调试选项,您必须快速更改配置文件。如果你迷路了,你也可以阅读这个线程

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:

如果您想快速调试,我强烈建议您使用LadyBugBundle。这是一个很棒的工具,可以让你做这样的事情:

In your controller:

在您的控制器中:

ladybug_dump($searchTerms);

In your TWIG template:

在您的 TWIG 模板中:

{{ searchTerms|ladybug_dump }}

Not that different from a classic var_dumpoption, but if you have long arrays or objects, ladybugwill 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:

与经典var_dump选项没有什么不同,但如果您有很长的数组或对象,ladybug会给您留下深刻印象。更重要的是,在控制器中,您经常需要在某个点停止代码以避免在调试语句后加载页面,这对于ladybug 来说相当容易:

ladybug_dump_die($searchTerms);

You can even ask ladybugto load the "debugged" variable into Symfony profiler with this simple statement.

您甚至可以使用这个简单的语句让ladybug将“调试”变量加载到Symfony 分析器中。

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

You have now direct access of the variable from a tab of the Symfony2 profiler. Ladybugcan do a lot more, but for this, the docis really good.

您现在可以从 Symfony2 分析器的选项卡直接访问变量。 Ladybug可以做更多的事情,但对于这一点,文档非常好。

回答by user79382

I think you must change template like this:

我认为您必须像这样更改模板:

{% for item in searchTerms %}
    {{ item.color }}<br/>
{% endfor %}

See official documentation: Creating and using Templates->embedding-controllers

参见官方文档:Creating and using Templates->embedding-controllers