Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

Default location to Twig templates is app/Resources/views but if you wish to change it to classic path src/AppBundle/Resources/views then add following line to configuration file.


# app/config/config.yml

twig:
...
paths: ["%kernel.project_dir%/src/AppBundle/Resources/views"]

If you inject @templating service into your controller to serve Twig templates then add following line to configuration file.


# app/config/config.yml

framework:
templating:
engines: ['twig']

After these changes, you can have a controller like show below.


namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;

class DefaultController
{
private $templating;

public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}

public function indexAction()
{
$template = $this->templating->render('AppBundle:default:index.html.twig', [...]);

return (new Response())->setContent($template);
}
}

# src/AppBundle/Resources/views/default/index.html.twig

{% extends '::base.html.twig' %}

{% block body %}
Default
{% endblock %}