0
votes

I have created a custom template that I am trying to render from a controller. I have placed the template in src/MyCompany/AppBundle/Contact/contact.html.twig and am using this line to try to bring it in:

    return $this->render('AppBundle:Contact:contact.html.twig', array(
        'form' => $form->createView()
    ));

But I get this InvalidArgumentException when loading the controller:

Unable to find template "AppBundle:Contact:contact.html.twig" (looked into: /usr/src/app/app/Resources/views, /usr/src/app/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form, /usr/src/app/vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views).

Is there a simple way to find out how I should be referencing this template?

1

1 Answers

1
votes

By default Symfony will look in the Resources/views directories of your bundles to find the templates. If you need a custom path like src/MyCompany/AppBundle/Contact you'll have to manually register it in twig configuration in your config.yml:

twig:
     # ...
     paths:
        '%kernel.root_dir%/src/MyCompany/AppBundle/Contact': contact_path

and then use it like this:

return $this->render('contact_path:contact.html.twig', array(
    'form' => $form->createView()
));

Documentation here