-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[#1786] Adding documentation for Twig namespaced paths support #2211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ Templating | |
:maxdepth: 2 | ||
|
||
global_variables | ||
namespaced_paths | ||
PHP | ||
twig_extension | ||
render_without_controller |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
.. index:: | ||
single: Templating; Namespaced Twig Paths | ||
|
||
How to use and Register namespaced Twig Paths | ||
============================================= | ||
|
||
.. versionadded:: 2.2 | ||
Namespaced path support was added in 2.2. | ||
|
||
Usually, when you refer to a template, you'll use the ``MyBundle:Subdir:filename.html.twig`` | ||
format (see :ref:`template-naming-locations`). | ||
|
||
Twig also natively offers a feature called "namespaced paths", and support | ||
is built-in automatically for all of your bundles. | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one empty line to much |
||
Take the following paths as an example: | ||
|
||
.. code-block:: jinja | ||
|
||
{% extends "AcmeDemoBundle::layout.html.twig" %} | ||
{% include "AcmeDemoBundle:Foo:bar.html.twig" %} | ||
|
||
With namespaced paths, the following works as well: | ||
|
||
.. code-block:: jinja | ||
|
||
{% extends "@AcmeDemo/layout.html.twig" %} | ||
{% include "@AcmeDemo/Foo/bar.html.twig" %} | ||
|
||
Both paths are valid and functional by default in Symfony2. | ||
|
||
.. tip:: | ||
|
||
As an added bonus, the namespaced syntax is faster. | ||
|
||
Registering your own namespaces | ||
------------------------------- | ||
|
||
You can also register your own custom namespaces. Suppose that you're using | ||
some third-party library that includes Twig templates that live in | ||
``vendor/acme/foo-project/templates``. First, register a namespace for this | ||
directory: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config.yml | ||
twig: | ||
# ... | ||
paths: | ||
"%kernel.root_dir%/../vendor/acme/foo-bar/templates": foo_bar | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should remove this empty line |
||
<?xml version="1.0" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:twig="http://symfony.com/schema/dic/twig" | ||
> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This 'greater than' character should be in line with the start of the tag. |
||
|
||
<twig:config debug="%kernel.debug%" strict-variables="%kernel.debug%"> | ||
<twig:path namespace="foo_bar">%kernel.root_dir%/../vendor/acme/foo-bar/templates</twig:path> | ||
</twig:config> | ||
</container> | ||
|
||
|
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
$container->loadFromExtension('twig', array( | ||
'paths' => array( | ||
'%kernel.root_dir%/../vendor/acme/foo-bar/templates' => 'foo_bar' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there should be a comma at the end of this line |
||
); | ||
)); | ||
|
||
The registered namespace is called ``foo_bar``, which refers to the | ||
``vendor/acme/foo-project/templates`` directory. Assuming there's a file | ||
called ``sidebar.twig`` in that directory, you can use it easily: | ||
|
||
.. code-block:: jinja | ||
|
||
{% include '@foo_bar/side.bar.twig` %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
@AcmeDemo