我有一个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));
}
但它只加载第一个模板而不是第二个模板。
答案 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));
}