php Symfony2 - Twig 渲染控制器并返回数组响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16918738/
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 11:53:13  来源:igfitidea点击:

Symfony2 - Twig render controller and return array response

phpsymfonytwig

提问by Jimbo

Note: What I'm doing here is embedding controllers<--- see that link for a similar (official)example.

注意:我在这里做的是嵌入控制器<--- 有关类似(官方)示例,请参阅该链接。

I want to call a controller from a twig template, and have that controller return an arraythat I can then use throughout the rest of my template.

我想从树枝模板中调用控制器,并让该控制器返回一个数组,然后我可以在模板的其余部分中使用该数组

I can do this with individual variables:

我可以用单个变量来做到这一点:

Twig

枝条

{% set testVar = render(controller('AppBundle:Test:index')) %}

Controller

控制器

class TestController extends Controller
{
    public function testAction()
    {
        return new Response('OH HAI');
    }
}

However, the following throws an exception: ("The Response content must be a string or object implementing __toString(), "array" given.")with the same twig file.

但是,以下会引发异常:("The Response content must be a string or object implementing __toString(), "array" given.")使用相同的 twig 文件.

public function testAction()
{
    return new Response(array('test' => 1, 'foo' => 'bar'));
}

This throws the above exception. How can I accomplish that which I seek without creating a dummy, useless extra template for the controller to render?

这会引发上述异常。我怎样才能在不为控制器渲染创建一个虚拟的、无用的额外模板的情况下完成我所寻求的?

采纳答案by Cyprian

The standard way to achieve what you want looks something like that.

实现您想要的东西的标准方法看起来像这样。

Lets assume that you have your regular action. Eg.

让我们假设您有常规操作。例如。

class TestController extends Controller
{
    public function testAction()
    {
        return $this->render('AppBundle:Test:index.html.twig');
    }
}

And the template:

和模板:

<html>
    <body>
        {% block sidebar %}
            {{ controller('AppBundle:Test:sidebar') }}
        {% endblock %}
        {% block content %}
            Hello world
        {% endblock %}                    
    </body>
</html>

Next you need create some action for the sidebar. Note that in this way you avoid put any logic into your view layer.

接下来,您需要为侧边栏创建一些操作。请注意,通过这种方式,您可以避免将任何逻辑放入视图层。

class BaseController extends Controller
{
    public function sidebarAction()
    {
        $status = $this->get('some.status.logic')->retrieveStatus();

        return $this->render('AppBundle:Base:sidebar.html.twig', array(
            'status' => $status,
        ));
    }
}

And your Base/sidebar.html.twig:

还有你的Base/sidebar.html.twig

<div class="sidebar">
   {{ status.showStatusInfo() }}
</div>

And that's all. You're not breaking MVP in that way, because you still don't have any logic in your view layer (the logic for the sidebar is in BaseController).

就这样。您并没有以这种方式破坏 MVP,因为您的视图层中仍然没有任何逻辑(侧边栏的逻辑在 中BaseController)。