Skip to content

Finishing the Templating component docs #3589

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

Merged
merged 4 commits into from
Mar 9, 2014
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions components/templating/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------

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

Global Variables
----------------

Sometimes, you need to set a variable which is available in all templates
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove the comma.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it both ways on the net when I search for "Sometimes, you need to" - so I'm cool either way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really? Well, you are the native. If you don't object, then let's stick with this. :)

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

<p>The google tracking code is: <?php echo $ga_tracking ?></p>

.. 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
---------------

Expand Down Expand Up @@ -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`
interface. This interface requires 3 method:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove some words "interface":

implements :class:Symfony\\Component\\Templating\\EngineInterface. This
requires 3 methods:


* :method:`render($name, array $parameters = array()) <Symfony\\Component\\Templating\\EngineInterface::render>`
- Renders a template
* :method:`exists($name) <Symfony\\Component\\Templating\\EngineInterface::exists>`
- Checks if the template exists
* :method:`supports($name) <Symfony\\Component\\Templating\\EngineInterface::supports>`
- Checks if the given template can be handled by this engine.

Using Multiple Engines
----------------------

It is possible to use multiple engines at the same time using the
:class:`Symfony\\Component\\Templating\\DelegatingEngine` class. This class
takes a list of engines and acts just like a normal templating engine. The
only difference is that it delegates the calls to one of the other engines. To
choose which one to use for the template, the
:method:`EngineInterface::supports() <Symfony\\Component\\Templating\\EngineInterface::supports>`
method is used.

.. code-block:: php

use Acme\Templating\CustomEngine;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\DelegatingEngine;

$templating = new DelegatingEngine(array(
new PhpEngine(...),
new CustomEngine(...)
));

.. _Packagist: https://packagist.org/packages/symfony/templating