8000 Merge pull request #1151 from GromNaN/2.0-templating · marijn/symfony-docs@f5e52f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5e52f8

Browse files
committed
Merge pull request symfony#1151 from GromNaN/2.0-templating
[Templating] Create documentation for Templating component
2 parents d732f91 + bc25281 commit f5e52f8

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

components/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
* :doc:`/components/locale`
1010
* :doc:`/components/process`
1111
* :doc:`/components/routing`
12+
* :doc:`/components/templating`
1213
* :doc:`/components/yaml`

components/templating.rst

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
< 8000 code>1+
.. index::
2+
single: Templating
3+
4+
The Templating Component
5+
========================
6+
7+
Templating provides all the tools needed to build any kind of template
8+
system.
9+
10+
It provides an infrastructure to load template files and optionally monitor
11+
them for changes. It also provides a concrete template engine implementation
12+
using PHP with additional tools for escaping and separating templates into
13+
blocks and layouts.
14+
15+
Installation
16+
------------
17+
18+
You can install the component in many different ways:
19+
20+
* Use the official Git repository (https://github.com/symfony/Templating);
21+
* Install it via PEAR (`pear.symfony.com/Templating`);
22+
* Install it via Composer (`symfony/templating` on Packagist).
23+
24+
Usage
25+
-----
26+
27+
The :class:`Symfony\\Component\\Templating\\PhpEngine` class is the entry point
28+
of the component. It needs a template name parser
29+
(:class:`Symfony\\Component\\templating\\TemplateNameParserInterface`) to
30+
convert a template name to a template reference and template loader
31+
(:class:`Symfony\\Component\\templating\\Loader\\LoaderInterface`) to find the
32+
template associated to a reference.
33+
34+
use Symfony\Component\Templating\PhpEngine;
35+
use Symfony\Component\Templating\TemplateNameParser;
36+
use Symfony\Component\Templating\Loader\FilesystemLoader;
37+
38+
$loader = new FilesystemLoader(__DIR__ . '/views/%name%');
39+
40+
$view = new PhpEngine(new TemplateNameParser(), $loader);
41+
42+
echo $view->render('hello.php', array('firstname' => 'Fabien'));
43+
44+
The :method:`Symfony\\Component\\Templating\\PhpEngine::render` method executes
45+
the file `views/hello.php` and returns the output text.
46+
47+
.. code-block::php
48+
49+
<!-- views/hello.php -->
50+
Hello, <?php echo $firstname ?>!
51+
52+
53+
Template inheritence with slots
54+
-------------------------------
55+
56+
The template inheritence is designed to share layouts with many templates.
57+
58+
.. code-block::php
59+
60+
<!-- views/layout.php -->
61+
<html>
62+
<head>
63+
<title><?php $view['slots']->output('title', 'Default title') ?></title>
64+
</head>
65+
<body>
66+
<?php $view['slots']->output('_content') ?>
67+
</body>
68+
</html>
69 8000 +
70+
The :method:`Symfony\\Templating\\PhpEngine::extend` method is called in the
71+
sub-template to set its parent template.
72+
73+
.. code-block::php
74+
75+
<!-- views/page.php -->
76+
<?php $view->extend('layout.php') ?>
77+
78+
<?php $view['slots']->set('title', $page->title) ?>
79+
80+
<h1>
81+
<?php echo $page->title ?>
82+
</h1>
83+
<p>
84+
<?php echo $page->body ?>
85+
</p>
86+
87+
To use template inheritence, the :class:`Symfony\\Templating\\Helper\\SlotsHelper`
88+
helper must be registered.
89+
90+
use Symfony\Templating\Helper\SlotsHelper;
91+
92+
$view->set(new SlotsHelper());
93+
94+
// Retrieve $page object
95+
96+
echo $view->render('page.php', array('page' => $page));
97+
98+
.. note::
99+
100+
Multiple levels of inheritence are possible: a layout can extend an other
101+
layout.
102+

0 commit comments

Comments
 (0)
0