In Symfony application, Twig templates are stored in the templates
directory by default. There might be required to store the Twig templates in different location. This tutorial shows how to set default path for Twig templates in Symfony 7 application.
The default templates directory can be configured using the default_path
option under the twig
key:
twig:
default_path: '%kernel.project_dir%/client_templates'
Now, Symfony will look Twig templates in the specified directory by default. To test, create the controller and template in a new directory.
{{ 'Testing new default directory' }}
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class TestController extends AbstractController
{
#[Route('/')]
public function index(): Response
{
return $this->render('test/index.html.twig');
}
}
Leave a Comment
Cancel reply