Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Cookbook
  4. Templating
  5. How to Use and Register Namespaced Twig Paths
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Registering your own Namespaces
    • Multiple Paths per Namespace

How to Use and Register Namespaced Twig Paths

Edit this page

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

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

How to Use and Register Namespaced Twig Paths

2.2

Namespaced path support was introduced in 2.2.

Usually, when you refer to a template, you'll use the MyBundle:Subdir:filename.html.twig format (see Creating and Using Templates).

Twig also natively offers a feature called "namespaced paths", and support is built-in automatically for all of your bundles.

Take the following paths as an example:

1
2
{% extends "AppBundle::layout.html.twig" %}
{{ include('AppBundle:Foo:bar.html.twig') }}

With namespaced paths, the following works as well:

1
2
{% extends "@App/layout.html.twig" %}
{{ include('@App/Foo/bar.html.twig') }}

Both paths are valid and functional by default in Symfony.

Tip

As an added bonus, the namespaced syntax is faster.

Registering your own Namespaces

You can also register your own custom namespaces. Suppose that you're using some third-party library that includes Twig templates that live in vendor/acme/foo-bar/templates. First, register a namespace for this directory:

1
2
3
4
5
# app/config/config.yml
twig:
    # ...
    paths:
        "%kernel.root_dir%/../vendor/acme/foo-bar/templates": foo_bar
1
2
3
4
5
6
7
8
9
10
11
<!-- app/config/config.xml -->
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:twig="http://symfony.com/schema/dic/twig"
>

    <twig:config debug="%kernel.debug%" strict-variables="%kernel.debug%">
        <twig:path namespace="foo_bar">%kernel.root_dir%/../vendor/acme/foo-bar/templates</twig:path>
    </twig:config>
</container>
1
2
3
4
5
6
// app/config/config.php
$container->loadFromExtension('twig', array(
    'paths' => array(
        '%kernel.root_dir%/../vendor/acme/foo-bar/templates' => 'foo_bar',
    );
));

Caution

Prior to 2.8, templates in custom namespaces are not pre-compiled by Symfony's cache warmup process. They are compiled on demand. This may cause problems if two simultaneous requests are trying to use the template for the first time.

The registered namespace is called foo_bar, which refers to the vendor/acme/foo-bar/templates directory. Assuming there's a file called sidebar.twig in that directory, you can use it easily:

1
{{ include('@foo_bar/sidebar.twig') }}

Multiple Paths per Namespace

You can also assign several paths to the same template namespace. The order in which paths are configured is very important, because Twig will always load the first template that exists, starting from the first configured path. This feature can be used as a fallback mechanism to load generic templates when the specific template doesn't exist.

1
2
3
4
5
6
7
# app/config/config.yml
twig:
    # ...
    paths:
        "%kernel.root_dir%/../vendor/acme/themes/theme1": theme
        "%kernel.root_dir%/../vendor/acme/themes/theme2": theme
        "%kernel.root_dir%/../vendor/acme/themes/common": theme
1
2
3
4
5
6
7
8
9
10
11
12
<!-- app/config/config.xml -->
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:twig="http://symfony.com/schema/dic/twig"
>

    <twig:config debug="%kernel.debug%" strict-variables="%kernel.debug%">
        <twig:path namespace="theme">%kernel.root_dir%/../vendor/acme/themes/theme1</twig:path>
        <twig:path namespace="theme">%kernel.root_dir%/../vendor/acme/themes/theme2</twig:path>
        <twig:path namespace="theme">%kernel.root_dir%/../vendor/acme/themes/common</twig:path>
    </twig:config>
</container>
1
2
3
4
5
6
7
8
// app/config/config.php
$container->loadFromExtension('twig', array(
    'paths' => array(
        '%kernel.root_dir%/../vendor/acme/themes/theme1' => 'theme',
        '%kernel.root_dir%/../vendor/acme/themes/theme2' => 'theme',
        '%kernel.root_dir%/../vendor/acme/themes/common' => 'theme',
    ),
));

Now, you can use the same @theme namespace to refer to any template located in the previous three directories:

1
{{ include('@theme/header.twig') }}
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Measure & Improve Symfony Code Performance

    Measure & Improve Symfony Code Performance

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Symfony footer

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

    Avatar of ackerman, a Symfony contributor

    Thanks ackerman for being a Symfony contributor

    1 commit • 4 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 Meilisearch