|
| 1 | +.. index:: |
| 2 | + single: Templating; Namespaced Twig Paths |
| 3 | + |
| 4 | +How to use and Register namespaced Twig Paths |
| 5 | +============================================= |
| 6 | + |
| 7 | +.. versionadded:: 2.2 |
| 8 | + Namespaced path support was added in 2.2. |
| 9 | + |
| 10 | +Usually, when you refer to a template, you'll use the ``MyBundle:Subdir:filename.html.twig`` |
| 11 | +format (see :ref:`template-naming-locations`). |
| 12 | + |
| 13 | +Twig also natively offers a feature called "namespaced paths", and support |
| 14 | +is built-in automatically for all of your bundles. |
| 15 | + |
| 16 | + |
| 17 | +Take the following paths as an example: |
| 18 | + |
| 19 | +.. code-block:: jinja |
| 20 | +
|
| 21 | + {% extends "AcmeDemoBundle::layout.html.twig" %} |
| 22 | + {% include "AcmeDemoBundle:Foo:bar.html.twig" %} |
| 23 | +
|
| 24 | +With namespaced paths, the following works as well: |
| 25 | + |
| 26 | +.. code-block:: jinja |
| 27 | +
|
| 28 | + {% extends "@AcmeDemo/layout.html.twig" %} |
| 29 | + {% include "@AcmeDemo/Foo/bar.html.twig" %} |
| 30 | +
|
| 31 | +Both paths are valid and functional by default in Symfony2. |
| 32 | + |
| 33 | +.. tip:: |
| 34 | + |
| 35 | + As an added bonus, the namespaced syntax is faster. |
| 36 | + |
| 37 | +Registering your own namespaces |
| 38 | +------------------------------- |
| 39 | + |
| 40 | +You can also register your own custom namespaces. Suppose that you're using |
| 41 | +some third-party library that includes Twig templates that live in |
| 42 | +``vendor/acme/foo-project/templates``. First, register a namespace for this |
| 43 | +directory: |
| 44 | + |
| 45 | +.. configuration-block:: |
| 46 | + |
| 47 | + .. code-block:: yaml |
| 48 | +
|
| 49 | + # app/config/config.yml |
| 50 | + twig: |
| 51 | + # ... |
| 52 | + paths: |
| 53 | + "%kernel.root_dir%/../vendor/acme/foo-bar/templates": foo_bar |
| 54 | +
|
| 55 | + .. code-block:: xml |
| 56 | +
|
| 57 | + <!-- app/config/config.xml --> |
| 58 | +
|
| 59 | + <?xml version="1.0" ?> |
| 60 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 61 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 62 | + xmlns:twig="http://symfony.com/schema/dic/twig" |
| 63 | + > |
| 64 | +
|
| 65 | + <twig:config debug="%kernel.debug%" strict-variables="%kernel.debug%"> |
| 66 | + <twig:path namespace="foo_bar">%kernel.root_dir%/../vendor/acme/foo-bar/templates</twig:path> |
| 67 | + </twig:config> |
| 68 | + </container> |
| 69 | +
|
| 70 | +
|
| 71 | + .. code-block:: php |
| 72 | +
|
| 73 | + // app/config/config.php |
| 74 | + $container->loadFromExtension('twig', array( |
| 75 | + 'paths' => array( |
| 76 | + '%kernel.root_dir%/../vendor/acme/foo-bar/templates' => 'foo_bar' |
| 77 | + ); |
| 78 | + )); |
| 79 | +
|
| 80 | +The registered namespace is called ``foo_bar``, which refers to the |
| 81 | +``vendor/acme/foo-project/templates`` directory. Assuming there's a file |
| 82 | +called ``sidebar.twig`` in that directory, you can use it easily: |
| 83 | + |
| 84 | +.. code-block:: jinja |
| 85 | +
|
| 86 | + {% include '@foo_bar/side.bar.twig` %} |
0 commit comments