diff --git a/components/templating/introduction.rst b/components/templating/introduction.rst index 130358677a9..12c5e29f790 100644 --- a/components/templating/introduction.rst +++ b/components/templating/introduction.rst @@ -52,6 +52,12 @@ the ``views/hello.php`` file and returns the output text. The second argument of ``render`` is an array of variables to use in the template. In this example, the result will be ``Hello, Fabien!``. +.. note:: + + Templates will be cached in the memory of the engine. This means that if + you render the same template multiple times in the same request, the + template will only be loaded once from the file system. + The ``$view`` variable ---------------------- @@ -73,6 +79,33 @@ to render the template originally) inside the template to render another templat render('hello.php', array('firstname' => $name)) ?> +Global Variables +---------------- + +Sometimes, you need to set a variable which is available in all templates +rendered by an engine (like the ``$app`` variable when using the Symfony2 +framework). These variables can be set by using the +:method:`Symfony\\Component\\Templating\\PhpEngine::addGlobal` method and they +can be accessed in the template as normal variables:: + + $templating->addGlobal('ga_tracking', 'UA-xxxxx-x'); + +In a template: + +.. code-block:: html+php + +
The google tracking code is:
+ +.. caution:: + + The global variables cannot be called ``this`` or ``view``, since they are + already used by the PHP engine. + +.. note:: + + The global variables can be overriden by a local variable in the template + with the same name. + Output Escaping --------------- @@ -128,4 +161,41 @@ The ``Helper`` has one required method: :method:`Symfony\\Component\\Templating\\Helper\\HelperInterface::getName`. This is the name that is used to get the helper from the ``$view`` object. +Creating a Custom Engine +------------------------ + +Besides providing a PHP templating engine, you can also create your own engine +using the Templating component. To do that, create a new class which +implements the :class:`Symfony\\Component\\Templating\\EngineInterface`. This +requires 3 method: + +* :method:`render($name, array $parameters = array())