Triyugi Narayan Mani
Triyugi Narayan Mani

Reputation: 3109

How to load two templates inside controller function?

I have a list.html.twig file in which I have included another template file like:

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

And in my controller function following is given:

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));
}

But it only loading the first template and not the second.

Upvotes: 0

Views: 75

Answers (1)

Sylvain Guilbert
Sylvain Guilbert

Reputation: 722

includes should be written like so :

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

and in your controller you render only the parent template and pass the params you need to the child template

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));
}

Upvotes: 1

Related Questions