如何在控制器功能内加载两个模板?

时间:2016-06-23 13:21:08

标签: symfony

我有一个list.html.twig文件,其中包含了另一个模板文件,如:

<div class="panel-body">
    {{ include('default/partials/groupSettings.html.twig') }}
</div>

在我的控制器功能中给出了以下内容:

public function settingsListAction()
{
    $settingsGroup = $this->getDoctrine()->getRepository('AppBundle:J1SettingGroup')->findAll();

    return $this->render('default/settingsList.html.twig', array('settingsGroup' => $settingsGroup));

    $settingsList = $this->getDoctrine()->getRepository('AppBundle:J1Setting')->findAll();

    return $this->render('default/partials/groupSettings.html.twig', array('settingsList' => $settingsList));
}

但它只加载第一个模板而不是第二个模板。

1 个答案:

答案 0 :(得分:1)

包括应该这样写:

{{ include('[BundleName]:[directory_with_your_template]:templatename.html.twig', { 'settingsGroup': settingsGroup }) }}

并在控制器中只渲染父模板,并将所需的参数传递给子模板

public function settingsListAction()
{
    $settingsGroup = $this->getDoctrine()->getRepository('AppBundle:J1SettingGroup')->findAll();
    $settingsList = $this->getDoctrine()->getRepository('AppBundle:J1Setting')->findAll();

    return $this->render('default/settingsList.html.twig', array('settingsGroup' => $settingsGroup, 'settingsList' => $settingsList));
}