Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9fddab6

Browse filesBrowse files
committed
feature #3877 Added a note about configuring several paths under the same namespace (javiereguiluz)
This PR was merged into the 2.3 branch. Discussion ---------- Added a note about configuring several paths under the same namespace | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | 2.3+ | Fixed tickets | #3661 Commits ------- fb5388c Added a comma 40524ca Fixed the case of the headings 857c6cd Minor simplification of the XML code 29863f1 Added a note about configuring several paths under the same namespace
2 parents 302fa82 + fb5388c commit 9fddab6
Copy full SHA for 9fddab6

File tree

Expand file treeCollapse file tree

1 file changed

+54
-2
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+54
-2
lines changed

‎cookbook/templating/namespaced_paths.rst

Copy file name to clipboardExpand all lines: cookbook/templating/namespaced_paths.rst
+54-2Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. index::
22
single: Templating; Namespaced Twig Paths
33

4-
How to use and Register namespaced Twig Paths
4+
How to Use and Register Namespaced Twig Paths
55
=============================================
66

77
.. versionadded:: 2.2
@@ -33,7 +33,7 @@ Both paths are valid and functional by default in Symfony2.
3333

3434
As an added bonus, the namespaced syntax is faster.
3535

36-
Registering your own namespaces
36+
Registering your own Namespaces
3737
-------------------------------
3838

3939
You can also register your own custom namespaces. Suppose that you're using
@@ -81,3 +81,55 @@ called ``sidebar.twig`` in that directory, you can use it easily:
8181
.. code-block:: jinja
8282
8383
{% include '@foo_bar/sidebar.twig' %}
84+
85+
Multiple Paths per Namespace
86+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87+
88+
You can also assign several paths to the same template namespace. The order in
89+
which paths are configured is very important, because Twig will always load
90+
the first template that exists, starting from the first configured path. This
91+
feature can be used as a fallback mechanism to load generic templates when the
92+
specific template doesn't exist.
93+
94+
.. code-block:: yaml
95+
96+
# app/config/config.yml
97+
twig:
98+
# ...
99+
paths:
100+
"%kernel.root_dir%/../vendor/acme/themes/theme1": theme
101+
"%kernel.root_dir%/../vendor/acme/themes/theme2": theme
102+
"%kernel.root_dir%/../vendor/acme/themes/common": theme
103+
104+
.. code-block:: xml
105+
106+
<!-- app/config/config.xml -->
107+
<?xml version="1.0" ?>
108+
<container xmlns="http://symfony.com/schema/dic/services"
109+
xmlns:twig="http://symfony.com/schema/dic/twig"
110+
>
111+
112+
<twig:config debug="%kernel.debug%" strict-variables="%kernel.debug%">
113+
<twig:path namespace="theme">%kernel.root_dir%/../vendor/acme/themes/theme1</twig:path>
114+
<twig:path namespace="theme">%kernel.root_dir%/../vendor/acme/themes/theme2</twig:path>
115+
<twig:path namespace="theme">%kernel.root_dir%/../vendor/acme/themes/common</twig:path>
116+
</twig:config>
117+
</container>
118+
119+
.. code-block:: php
120+
121+
// app/config/config.php
122+
$container->loadFromExtension('twig', array(
123+
'paths' => array(
124+
'%kernel.root_dir%/../vendor/acme/themes/theme1' => 'theme',
125+
'%kernel.root_dir%/../vendor/acme/themes/theme2' => 'theme',
126+
'%kernel.root_dir%/../vendor/acme/themes/common' => 'theme',
127+
);
128+
));
129+
130+
Now, you can use the same ``@theme`` namespace to refer to any template located
131+
in the previous three directories:
132+
133+
.. code-block:: jinja
134+
135+
{% include '@theme/header.twig' %}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.