Skip to content

Commit 0eda298

Browse files
committed
[#1786] Adding documentation for Twig namespaced paths support
1 parent eedcf38 commit 0eda298

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)