Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Best Practices
  4. Templates
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Template Locations
  • Twig Extensions

Templates

Edit this page

Warning: You are browsing the documentation for Symfony 4.0, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

Templates

When PHP was created 20 years ago, developers loved its simplicity and how well it blended HTML and dynamic code. But as time passed, other template languages - like Twig - were created to make templating even better.

Best Practice

Use Twig templating format for your templates.

Generally speaking, PHP templates are more verbose than Twig templates because they lack native support for lots of modern features needed by templates, like inheritance, automatic escaping and named arguments for filters and functions.

Twig is the default templating format in Symfony and has the largest community support of all non-PHP template engines (it's used in high profile projects such as Drupal 8).

Template Locations

Best Practice

Store the application templates in the templates/ directory at the root of your project.

Centralizing your templates in a single location simplifies the work of your designers. In addition, using this directory simplifies the notation used when referring to templates (e.g. $this->render('admin/post/show.html.twig') instead of $this->render('@SomeTwigNamespace/Admin/Posts/show.html.twig')).

Best Practice

Use lowercased snake_case for directory and template names.

This recommendation aligns with Twig best practices, where variables and template names use lowercased snake_case too (e.g. user_profile instead of userProfile and edit_form.html.twig instead of EditForm.html.twig).

Best Practice

Use a prefixed underscore for partial templates in template names.

You often want to reuse template code using the include function to avoid redundant code. To determine those partials easily in the filesystem you should prefix partials and any other template without HTML body or extends tag with a single underscore.

Twig Extensions

Best Practice

Define your Twig extensions in the src/Twig/ directory. Your application will automatically detect them and configure them.

Our application needs a custom md2html Twig filter so that we can transform the Markdown contents of each post into HTML. To do this, create a new Markdown class that will be used later by the Twig extension. It just needs to define one single method to transform Markdown content into HTML:

1
2
3
4
5
6
7
8
9
10
11
namespace App\Utils;

class Markdown
{
    // ...

    public function toHtml(string $text): string
    {
        return $this->parser->text($text);
    }
}

Next, create a new Twig extension and define a filter called md2html using the Twig\TwigFilter class. Inject the newly defined Markdown class in the constructor of the Twig extension:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
namespace App\Twig;

use App\Utils\Markdown;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class AppExtension extends AbstractExtension
{
    private $parser;

    public function __construct(Markdown $parser)
    {
        $this->parser = $parser;
    }

    public function getFilters()
    {
        return [
            new TwigFilter('md2html', [$this, 'markdownToHtml'], [
                'is_safe' => ['html'],
                'pre_escape' => 'html',
            ]),
        ];
    }

    public function markdownToHtml($content)
    {
        return $this->parser->toHtml($content);
    }
}

And that's it!

If you're using the default services.yaml configuration, you're done! Symfony will automatically know about your new service and tag it to be used as a Twig extension.


Next: Forms

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Online Symfony certification, take it now!

Online Symfony certification, take it now!

Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Moroine Bentefrit, a Symfony contributor

Thanks Moroine Bentefrit for being a Symfony contributor

1 commit • 48 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia