@@ -17,6 +17,8 @@ Installation & Configuration
17
17
18
18
Enable the ``TwigBundle `` in your kernel::
19
19
20
+ // hello/config/HelloKernel.php
21
+
20
22
public function registerBundles()
21
23
{
22
24
$bundles = array(
@@ -72,6 +74,8 @@ To render a Twig template instead of a PHP one, add the ``:twig`` suffix at the
72
74
end of the template name. The controller below renders the ``index.twig ``
73
75
template::
74
76
77
+ // src/Application/HelloBundle/Controller/HelloController.php
78
+
75
79
public function indexAction($name)
76
80
{
77
81
return $this->render('HelloBundle:Hello:index:twig', array('name' => $name));
@@ -83,38 +87,101 @@ Symfony2 automatically switches the default engine to Twig:
83
87
84
88
.. code-block :: jinja
85
89
86
- {# index.twig #}
90
+ {# src/Application/HelloBundle/Resources/views/Hello/ index.twig #}
87
91
88
92
{# no need to add :twig as this is the default #}
89
- {% extends 'HelloBundle::layout' %}
93
+ {% extends "HelloBundle::layout" %}
94
+
95
+ Hello {{ $name }}!
96
+
97
+ .. note ::
98
+ The Twig templates must use the ``twig `` extension.
99
+
100
+ And here is a typical layout:
101
+
102
+ .. code-block :: jinja
90
103
91
- {% block content %}
92
- Hello {{ name }}
104
+ {# src/Application/HelloBundle/Resources/views/layout.twig #}
105
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
106
+ <html>
107
+ <head>
108
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
109
+ </head>
110
+ <body>
111
+ {% block body %}{% endblock %}
112
+ </body>
113
+ </html>
93
114
94
- {# use the special render tag to render a template #}
95
- {% render 'HelloBundle:Hello:sidebar' %}
96
- {% endblock %}
115
+ Include other Templates
116
+ -----------------------
97
117
98
- To embed a PHP template in a Twig one, add the ``:php `` suffix to the template
99
- name:
118
+ The best way to share a snippet of code between several distinct templates is
119
+ to define a template that can then be included into another one.
120
+
121
+ Create a ``hello.twig `` template:
100
122
101
123
.. code-block :: jinja
102
124
103
- {# index.twig #}
125
+ {# src/Application/HelloBundle/Resources/views/Hello/hello.twig #}
126
+ Hello {{ $name }}
127
+
128
+ And change the ``index.twig `` template to include it:
129
+
130
+ .. code-block :: jinja
131
+
132
+ {# src/Application/HelloBundle/Resources/views/Hello/index.php #}
133
+ {% extends "HelloBundle::layout" %}
134
+
135
+ {% include "HelloBundle:Hello:hello" %}
136
+
137
+ .. tip:
138
+ To embed a PHP template in a Twig one, add the ``:php`` suffix to the template
139
+ name:
104
140
105
- {% render 'HelloBundle:Hello:sidebar:php' %}
141
+ .. code-block:: jinja
106
142
107
- And the opposite is also true::
143
+ {# index.twig #}
108
144
109
- // index. php
145
+ {% render 'HelloBundle:Hello:sidebar: php' %}
110
146
111
- <?php $view->render('HelloBundle:Hello:sidebar:twig') ?>
147
+ Embed other Actions
148
+ -------------------
149
+
150
+ And what if you want to embed the result of another action in a template?
151
+ That's very useful when working with Ajax, or when the embedded template needs
152
+ some variable not available in the main template.
153
+
154
+ If you create a ``fancy `` action, and want to include it into the ``index ``
155
+ template, simply use the following code:
156
+
157
+ .. code-block :: jinja
158
+
159
+ <!-- src/Application/HelloBundle/Resources/views/Hello/index.php -->
160
+ {% render "HelloBundle:Hello:fancy" with ['name': name, 'color': 'green'] %}
161
+
162
+ Here, the ``HelloBundle:Hello:fancy `` string refers to the ``fancy `` action of the
163
+ ``Hello `` controller::
164
+
165
+ // src/Application/HelloBundle/Controller/HelloController.php
166
+
167
+ class HelloController extends Controller
168
+ {
169
+ public function fancyAction($name, $color)
170
+ {
171
+ // create some object, based on the $color variable
172
+ $object = ...;
173
+
174
+ return $this->render('HelloBundle:Hello:fancy:twig', array('name' => $name, 'object' => $object));
175
+ }
176
+
177
+ // ...
178
+ }
112
179
113
180
.. index ::
114
181
single: Twig; Helpers
115
182
116
- Helpers
117
- -------
183
+ Template Helpers
184
+ ----------------
118
185
119
186
The default Symfony2 helpers are available within a Twig template via
120
187
specialized tags:
@@ -144,13 +211,13 @@ specialized tags:
144
211
{# embed another controller response #}
145
212
{% render 'BlogBundle:Post:list' with ['path': ['limit': 2], 'alt': 'BlogBundle:Post:error'] %}
146
213
147
- .. _ twig_extensions :
214
+ .. _ twig_extension_tag :
148
215
149
216
Enabling Custom Twig Extensions
150
217
-------------------------------
151
218
152
219
To enable a Twig extension, add it as a regular service in one of your
153
- configuration, and add a ``twig.extension `` annotation :
220
+ configuration, and tag it with ``twig.extension ``:
154
221
155
222
.. configuration-block ::
156
223
0 commit comments