@@ -24,15 +24,15 @@ your application configuration file:
24
24
25
25
.. code-block :: yaml
26
26
27
- # app/ config/config.yml
27
+ # config/packages/framework.yaml
28
28
framework :
29
29
# ...
30
30
templating :
31
31
engines : ['twig', 'php']
32
32
33
33
.. code-block :: xml
34
34
35
- <!-- app/ config/config .xml -->
35
+ <!-- config/packages/framework .xml -->
36
36
<?xml version =" 1.0" encoding =" UTF-8" ?>
37
37
<container xmlns =" http://symfony.com/schema/dic/services"
38
38
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -53,6 +53,7 @@ your application configuration file:
53
53
54
54
.. code-block :: php
55
55
56
+ // config/packages/framework.php
56
57
$container->loadFromExtension('framework', array(
57
58
// ...
58
59
'templating' => array(
@@ -67,28 +68,12 @@ below renders the ``index.html.php`` template::
67
68
// src/Controller/HelloController.php
68
69
69
70
// ...
70
- public function indexAction ($name)
71
+ public function index ($name)
71
72
{
72
- return $this->render(
73
- 'AppBundle:Hello:index.html.php',
74
- array('name' => $name)
75
- );
76
- }
77
-
78
- You can also use the `@Template `_ shortcut to render the default
79
- ``AppBundle:Hello:index.html.php `` template::
80
-
81
- // src/Controller/HelloController.php
82
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
83
-
84
- // ...
85
-
86
- /**
87
- * @Template(engine="php")
88
- */
89
- public function indexAction($name)
90
- {
91
- return array('name' => $name);
73
+ // template is stored in src/Resources/views/hello/index.html.php
74
+ return $this->render('hello/index.html.php', array(
75
+ 'name' => $name
76
+ ));
92
77
}
93
78
94
79
.. caution ::
@@ -98,24 +83,24 @@ You can also use the `@Template`_ shortcut to render the default
98
83
the ``@ `` notation for Twig namespaces will no longer be supported for the
99
84
``render() `` method::
100
85
101
- public function indexAction ()
86
+ public function index ()
102
87
{
103
88
// ...
104
89
105
90
// namespaced templates will no longer work in controllers
106
- $this->render('@App/Default /index.html.twig');
91
+ $this->render('@SomeNamespace/hello /index.html.twig');
107
92
108
93
// you must use the traditional template notation
109
- $this->render('AppBundle:Default: index.html.twig');
94
+ $this->render('hello/ index.html.twig');
110
95
}
111
96
112
97
.. code-block :: twig
113
98
114
99
{# inside a Twig template, namespaced templates work as expected #}
115
- {{ include('@App/Default /index.html.twig') }}
100
+ {{ include('@SomeNamespace/hello /index.html.twig') }}
116
101
117
102
{# traditional template notation will also work #}
118
- {{ include('AppBundle:Default: index.html.twig') }}
103
+ {{ include('hello/ index.html.twig') }}
119
104
120
105
121
106
.. index ::
@@ -134,36 +119,29 @@ the ``extend()`` call:
134
119
135
120
.. code-block :: html+php
136
121
137
- <!-- templates/Hello /index.html.php -->
138
- <?php $view->extend('AppBundle:: layout.html.php') ?>
122
+ <!-- src/Resources/views/hello /index.html.php -->
123
+ <?php $view->extend('layout.html.php') ?>
139
124
140
125
Hello <?php echo $name ?>!
141
126
142
- The ``AppBundle::layout.html.php `` notation sounds familiar, doesn't it? It
143
- is the same notation used to reference a template. The ``:: `` part simply
144
- means that the controller element is empty, so the corresponding file is
145
- directly stored under ``views/ ``.
146
-
147
127
Now, have a look at the ``layout.html.php `` file:
148
128
149
129
.. code-block :: html+php
150
130
151
- <!-- templates /layout.html.php -->
152
- <?php $view->extend(':: base.html.php') ?>
131
+ <!-- src/Resources/views /layout.html.php -->
132
+ <?php $view->extend('base.html.php') ?>
153
133
154
134
<h1>Hello Application</h1>
155
135
156
136
<?php $view['slots']->output('_content') ?>
157
137
158
- The layout is itself decorated by another one (``:: base.html.php ``). Symfony
138
+ The layout is itself decorated by another one (``base.html.php ``). Symfony
159
139
supports multiple decoration levels: a layout can itself be decorated by
160
- another one. When the bundle part of the template name is empty, views are
161
- looked for in the ``templates/ `` directory. This directory stores
162
- global views for your entire project:
140
+ another one:
163
141
164
142
.. code-block :: html+php
165
143
166
- <!-- templates /base.html.php -->
144
+ <!-- src/Resources/views /base.html.php -->
167
145
<!DOCTYPE html>
168
146
<html>
169
147
<head>
@@ -196,8 +174,8 @@ decorating the template. In the ``index.html.php`` template, define a
196
174
197
175
.. code-block :: html+php
198
176
199
- <!-- templates/Hello /index.html.php -->
200
- <?php $view->extend('AppBundle:: layout.html.php') ?>
177
+ <!-- src/Resources/views/hello /index.html.php -->
178
+ <?php $view->extend('layout.html.php') ?>
201
179
202
180
<?php $view['slots']->set('title', 'Hello World Application') ?>
203
181
@@ -207,7 +185,7 @@ The base layout already has the code to output the title in the header:
207
185
208
186
.. code-block :: html+php
209
187
210
- <!-- templates /base.html.php -->
188
+ <!-- src/Resources/views /base.html.php -->
211
189
<head>
212
190
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
213
191
<title><?php $view['slots']->output('title', 'Hello Application') ?></title>
@@ -238,17 +216,17 @@ Create a ``hello.html.php`` template:
238
216
239
217
.. code-block :: html+php
240
218
241
- <!-- templates/Hello /hello.html.php -->
219
+ <!-- src/Resources/views/hello /hello.html.php -->
242
220
Hello <?php echo $name ?>!
243
221
244
222
And change the ``index.html.php `` template to include it:
245
223
246
224
.. code-block :: html+php
247
225
248
- <!-- templates/Hello /index.html.php -->
249
- <?php $view->extend('AppBundle:: layout.html.php') ?>
226
+ <!-- src/Resources/views/hello /index.html.php -->
227
+ <?php $view->extend('layout.html.php') ?>
250
228
251
- <?php echo $view->render('AppBundle :Hello: hello.html.php', array('name' => $name)) ?>
229
+ <?php echo $view->render('hello/ hello.html.php', array('name' => $name)) ?>
252
230
253
231
The ``render() `` method evaluates and returns the content of another template
254
232
(this is the exact same method as the one used in the controller).
@@ -268,35 +246,17 @@ If you create a ``fancy`` action, and want to include it into the
268
246
269
247
.. code-block :: html+php
270
248
271
- <!-- templates/Hello /index.html.php -->
249
+ <!-- src/Resources/views/hello /index.html.php -->
272
250
<?php echo $view['actions']->render(
273
- new \S ymfony\C omponent\H ttpKernel\C ontroller\C ontrollerReference('AppBundle:Hello: fancy', array(
274
- 'name' => $name,
275
- 'color' => 'green',
276
- ))
251
+ new \S ymfony\C omponent\H ttpKernel\C ontroller\C ontrollerReference(
252
+ 'App\C ontroller\H elloController::fancy',
253
+ array(
254
+ 'name' => $name,
255
+ 'color' => 'green',
256
+ )
257
+ )
277
258
) ?>
278
259
279
- Here, the ``AppBundle:Hello:fancy `` string refers to the ``fancy `` action of the
280
- ``Hello `` controller::
281
-
282
- // src/Controller/HelloController.php
283
-
284
- class HelloController extends Controller
285
- {
286
- public function fancyAction($name, $color)
287
- {
288
- // create some object, based on the $color variable
289
- $object = ...;
290
-
291
- return $this->render('AppBundle:Hello:fancy.html.php', array(
292
- 'name' => $name,
293
- 'object' => $object
294
- ));
295
- }
296
-
297
- // ...
298
- }
299
-
300
260
But where is the ``$view['actions'] `` array element defined? Like
301
261
``$view['slots'] ``, it's called a template helper, and the next section tells
302
262
you more about those.
@@ -332,10 +292,10 @@ pattern:
332
292
333
293
.. code-block :: yaml
334
294
335
- # src/Resources/ config/routing.yml
336
- hello : # The route name
337
- path : /hello/{name}
338
- defaults : { _controller: AppBundle:Hello: index }
295
+ # config/routes.yaml
296
+ hello :
297
+ path : /hello/{name}
298
+ controller : App\Controller\HelloController:: index
339
299
340
300
Using Assets: Images, JavaScripts and Stylesheets
341
301
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments