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