-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
weaverryan
merged 4 commits into
symfony:2.3
from
wouterj:components-enhancing_templating
Mar 9, 2014
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | |
<?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 | ||
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 | ||
--------------- | ||
|
||
|
@@ -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: | ||
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. We can remove some words "interface":
|
||
|
||
* :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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'd remove the comma.
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.
I see it both ways on the net when I search for
"Sometimes, you need to"
- so I'm cool either wayThere 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.
Really? Well, you are the native. If you don't object, then let's stick with this. :)