Skip to content

Commit 1409445

Browse files
committed
Merge pull request #2211 from symfony/twig-namespaced-paths
[#1786] Adding documentation for Twig namespaced paths support
2 parents eedcf38 + a7ce478 commit 1409445

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

book/templating.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ When working with template inheritance, here are some tips to keep in mind:
376376
Template Naming and Locations
377377
-----------------------------
378378

379+
.. versionadded:: 2.2
380+
Namespaced path support was added in 2.2, allowing for template names
381+
like ``@AcmeDemoBundle/layout.html.twig``. See :doc:`/cookbook/templating/namespaced_paths`
382+
for more details.
383+
379384
By default, templates can live in two different locations:
380385

381386
* ``app/Resources/views/``: The applications ``views`` directory can contain

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
* :doc:`/cookbook/templating/index`
135135

136136
* :doc:`/cookbook/templating/global_variables`
137+
* :doc:`/cookbook/templating/namespaced_paths`
137138
* :doc:`/cookbook/templating/PHP`
138139
* :doc:`/cookbook/templating/twig_extension`
139140
* :doc:`/cookbook/templating/render_without_controller`

cookbook/templating/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Templating
55
:maxdepth: 2
66

77
global_variables
8+
namespaced_paths
89
PHP
910
twig_extension
1011
render_without_controller
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)