@@ -52,6 +52,12 @@ the ``views/hello.php`` file and returns the output text. The second argument
52
52
of ``render `` is an array of variables to use in the template. In this
53
53
example, the result will be ``Hello, Fabien! ``.
54
54
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
+
55
61
The ``$view `` variable
56
62
----------------------
57
63
@@ -73,6 +79,33 @@ to render the template originally) inside the template to render another templat
73
79
<?php echo $view->render('hello.php', array('firstname' => $name)) ?>
74
80
<?php endforeach ?>
75
81
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
+
76
109
Output Escaping
77
110
---------------
78
111
@@ -128,4 +161,41 @@ The ``Helper`` has one required method:
128
161
:method: `Symfony\\ Component\\ Templating\\ Helper\\ HelperInterface::getName `.
129
162
This is the name that is used to get the helper from the ``$view `` object.
130
163
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
+
131
201
.. _Packagist : https://packagist.org/packages/symfony/templating
0 commit comments