渲染控制器动作时通过参数更改模板? [英] Change template via parameter when render a controller action?

查看:37
本文介绍了渲染控制器动作时通过参数更改模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 CRUD 控制器,带有一个 indexAction() 来列出所有项目.现在我想在另一个模板中呈现此操作并更改 .

I got a simple CRUD-controller with a indexAction() to list all items. Now I want render this action within another template and change the .

{{ render(controller("MyBundle:MyEntity:index")) }}

是否有预定义的参数来更改模板?当然很容易传递这个值,但我不想重新发明轮子.

Is there a predefined parameter to change the template? Sure its easy to pass this value, but I don't want reinvent the wheel.

推荐答案

没有用于更改模板的本机参数,但您可以将参数传递给由 render 方法定义的操作...

There is no native parameter to change the template but you can pass an argument to the action defined by the render method ...

{ render(controller('MyBundle:MyEntity:index', { 'template': 'MyBundle:ControllerName:foo.html.twig' })) }}

...并在您的控制器操作中使用它来决定要呈现哪个模板.

... and use it inside your controller action to decide which template to render.

class MyEntityController
{
     public function indexAction($template = null)
     {
         // ... some code here

         $template = $template ? $template : 'MyBundle:ControllerName:index.html.twig';

         return $this->render(
             $template,
             array(
               'variable' => $variable,
             )
         );
     }

这篇关于渲染控制器动作时通过参数更改模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆