Skip to content

Commit 56bc266

Browse files
committed
feature #3589 Finishing the Templating component docs (WouterJ)
This PR was merged into the 2.3 branch. Discussion ---------- Finishing the Templating component docs | Q | A | --- | --- | Doc fix? | no | New docs? | yes | Applies to | all | Fixed tickets | #955 Fixed the remeaning tasks of #955 (comment) Commits ------- b215395 Applied comment from @weaverryan 9f34675 Documented custom engines and DelegatingEngine e243485 Added a note on caching 3a9f140 Added globals section
2 parents d881181 + b215395 commit 56bc266

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

components/templating/introduction.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ the ``views/hello.php`` file and returns the output text. The second argument
5252
of ``render`` is an array of variables to use in the template. In this
5353
example, the result will be ``Hello, Fabien!``.
5454

55+
.. note::
56+
57+
Templates will be cached in the memory of the engine. This means that if
58+
you render the same template multiple times in the same request, the
59+
template will only be loaded once from the file system.
60+
5561
The ``$view`` variable
5662
----------------------
5763

@@ -73,6 +79,33 @@ to render the template originally) inside the template to render another templat
7379
<?php echo $view->render('hello.php', array('firstname' => $name)) ?>
7480
<?php endforeach ?>
7581

82+
Global Variables
83+
----------------
84+
85+
Sometimes, you need to set a variable which is available in all templates
86+
rendered by an engine (like the ``$app`` variable when using the Symfony2
87+
framework). These variables can be set by using the
88+
:method:`Symfony\\Component\\Templating\\PhpEngine::addGlobal` method and they
89+
can be accessed in the template as normal variables::
90+
91+
$templating->addGlobal('ga_tracking', 'UA-xxxxx-x');
92+
93+
In a template:
94+
95+
.. code-block:: html+php
96+
97+
<p>The google tracking code is: <?php echo $ga_tracking ?></p>
98+
99+
.. caution::
100+
101+
The global variables cannot be called ``this`` or ``view``, since they are
102+
already used by the PHP engine.
103+
104+
.. note::
105+
106+
The global variables can be overriden by a local variable in the template
107+
with the same name.
108+
76109
Output Escaping
77110
---------------
78111

@@ -128,4 +161,41 @@ The ``Helper`` has one required method:
128161
:method:`Symfony\\Component\\Templating\\Helper\\HelperInterface::getName`.
129162
This is the name that is used to get the helper from the ``$view`` object.
130163

164+
Creating a Custom Engine
165+
------------------------
166+
167+
Besides providing a PHP templating engine, you can also create your own engine
168+
using the Templating component. To do that, create a new class which
169+
implements the :class:`Symfony\\Component\\Templating\\EngineInterface`. This
170+
requires 3 method:
171+
172+
* :method:`render($name, array $parameters = array()) <Symfony\\Component\\Templating\\EngineInterface::render>`
173+
- Renders a template
174+
* :method:`exists($name) <Symfony\\Component\\Templating\\EngineInterface::exists>`
175+
- Checks if the template exists
176+
* :method:`supports($name) <Symfony\\Component\\Templating\\EngineInterface::supports>`
177+
- Checks if the given template can be handled by this engine.
178+
179+
Using Multiple Engines
180+
----------------------
181+
182+
It is possible to use multiple engines at the same time using the
183+
:class:`Symfony\\Component\\Templating\\DelegatingEngine` class. This class
184+
takes a list of engines and acts just like a normal templating engine. The
185+
only difference is that it delegates the calls to one of the other engines. To
186+
choose which one to use for the template, the
187+
:method:`EngineInterface::supports() <Symfony\\Component\\Templating\\EngineInterface::supports>`
188+
method is used.
189+
190+
.. code-block:: php
191+
192+
use Acme\Templating\CustomEngine;
193+
use Symfony\Component\Templating\PhpEngine;
194+
use Symfony\Component\Templating\DelegatingEngine;
195+
196+
$templating = new DelegatingEngine(array(
197+
new PhpEngine(...),
198+
new CustomEngine(...)
199+
));
200+
131201
.. _Packagist: https://packagist.org/packages/symfony/templating

0 commit comments

Comments
 (0)