|
1 | 1 | .. index::
|
2 | 2 | single: Templating; Namespaced Twig Paths
|
3 | 3 |
|
4 |
| -How to use and Register namespaced Twig Paths |
| 4 | +How to Use and Register Namespaced Twig Paths |
5 | 5 | =============================================
|
6 | 6 |
|
7 | 7 | .. versionadded:: 2.2
|
@@ -33,7 +33,7 @@ Both paths are valid and functional by default in Symfony2.
|
33 | 33 |
|
34 | 34 | As an added bonus, the namespaced syntax is faster.
|
35 | 35 |
|
36 |
| -Registering your own namespaces |
| 36 | +Registering your own Namespaces |
37 | 37 | -------------------------------
|
38 | 38 |
|
39 | 39 | 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:
|
81 | 81 | .. code-block:: jinja
|
82 | 82 |
|
83 | 83 | {% 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