Current Location: Home> Latest Articles> How to Render Views Using the Twig Template Engine in Symfony Framework

How to Render Views Using the Twig Template Engine in Symfony Framework

M66 2025-06-18

How to Render Views Using the Twig Template Engine in Symfony Framework

The Symfony framework is one of the most popular PHP frameworks, offering powerful features and tools to improve development efficiency. Within the Symfony framework, the Twig template engine is the default view engine, designed to maintain template simplicity and readability. This article will explain how to use the Twig template engine to render views in Symfony.

Step 1: Install the Twig Package

First, you need to install the Twig package using Composer. Open your terminal, navigate to your Symfony project directory, and run the following command:

composer require twig/twig

Composer will automatically download and install the Twig package into your project's vendor directory.

Step 2: Configure the Twig Template Engine

In Symfony, you need to configure the Twig template engine. Open the `config/packages/twig.yaml` file in your project and add the following configuration:

twig:

default_path: '%kernel.project_dir%/templates'

This configuration sets the default path for Twig templates to the `templates` directory.

Step 3: Create a Twig Template File

Now, create a new Twig template file in the `templates` directory, for example, `hello.html.twig`. In this file, you can use Twig syntax to write the template code.

Sample code:

<!DOCTYPE html>
<html>
<head>
    <title>{{ title }}</title>
</head>
<body>
    <h1>Hello, {{ name }}!</h1>
</body>
</html>

This code defines a simple HTML page and uses Twig's double curly brace syntax to insert variables. These variables will be passed to the template later on.

Step 4: Render the View in the Controller

In Symfony, controllers handle requests and return the rendered views. To use the Twig template engine in the controller, you need to instantiate Twig and pass the template and data to it.

Sample code:

use SymfonyBundleFrameworkBundleControllerAbstractController;

use SymfonyComponentRoutingAnnotationRoute;

use TwigEnvironment;

class HelloController extends AbstractController

{

/**

* @Route("/hello/{name}", name="hello")

*/

public function index(Environment $twig, $name)

{

$template = 'hello.html.twig';

$data = [

'title' => 'Hello',

'name' => $name

];

return $twig->render($template, $data);

}

}

In this code, we created a controller named `HelloController` and defined an `index` method to handle requests. The method takes a `TwigEnvironment` instance and a request parameter. Inside the method, we call `$twig->render()` to render the specified template and return the rendered view to the user.

Conclusion

By following the steps above, you have learned how to use the Twig template engine to render views in the Symfony framework. Twig offers powerful features such as template inheritance, control flow statements, and built-in filters, which greatly simplify view development and improve efficiency. I hope this article helps you better understand how to use Symfony and Twig in your projects.