@@ -29,12 +29,11 @@ the first time you see Twig, you probably understand most of it:
29
29
<body>
30
30
<h1>{{ page_title }}</h1>
31
31
32
- {# this is the main navigation menu #}
33
- <ul id="navigation">
34
- {% for item in navigation %}
35
- <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
36
- {% endfor %}
37
- </ul>
32
+ {% if user.isLoggedIn %}
33
+ Hello {{ user.name }}!
34
+ {% endif %}
35
+
36
+ {# ... #}
38
37
</body>
39
38
</html>
40
39
@@ -74,6 +73,45 @@ to display numbers and dates, the template caching, etc. Read the
74
73
Creating Templates
75
74
------------------
76
75
76
+ Before explaining in detail how to create and render templates, look at the
77
+ following example for a quick overview of the whole process. First, you need to
78
+ create a new file in the ``templates/ `` directory to store the template contents:
79
+
80
+ .. code-block :: html+twig
81
+
82
+ {# templates/user/notifications.html.twig #}
83
+ <h1>Hello {{ user_first_name }}!</h1>
84
+ <p>You have {{ notifications|length }} new notifications.</p>
85
+
86
+ Then, create a :doc: `controller </controller >` that renders this template and
87
+ passes to it the needed variables::
88
+
89
+ // src/Controller/UserController.php
90
+ namespace App\Controller;
91
+
92
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
93
+
94
+ class UserController extends AbstractController
95
+ {
96
+ // ...
97
+
98
+ public function notifications()
99
+ {
100
+ // get the user information and notifications somehow
101
+ $userFirstName = '...';
102
+ $userNotifications = ['...', '...'];
103
+
104
+ // the template path is the relative file path from `templates/`
105
+ return $this->render('user/notifications.html.twig', [
106
+ // this array defines the variables passed to the template,
107
+ // where the key is the variable name and the value is the variable value
108
+ // (Twig recommends using snake_case variable names: 'foo_bar' instead of 'fooBar')
109
+ 'user_first_name' => $userFirstName,
110
+ 'notifications' => $userNotifications,
111
+ ]);
112
+ }
113
+ }
114
+
77
115
Template Naming
78
116
~~~~~~~~~~~~~~~
79
117
@@ -104,8 +142,9 @@ Template Variables
104
142
~~~~~~~~~~~~~~~~~~
105
143
106
144
A common need for templates is to print the values stored in the templates
107
- passed from the controller or service. That's why Twig provides quick access to
108
- all kinds of PHP variables. Consider the following template:
145
+ passed from the controller or service. Variables usually store objects and
146
+ arrays instead of strings, numbers and boolean values. That's why Twig provides
147
+ quick access to complex PHP variables. Consider the following template:
109
148
110
149
.. code-block :: html+twig
111
150
@@ -256,10 +295,13 @@ You can now use the ``asset()`` function:
256
295
257
296
.. code-block :: html+twig
258
297
298
+ {# the image lives at "public/images/logo.png" #}
259
299
<img src="{{ asset('images/logo.png') }}" alt="Symfony!"/>
260
300
301
+ {# the CSS file lives at "public/css/blog.css" #}
261
302
<link href="{{ asset('css/blog.css') }}" rel="stylesheet"/>
262
303
304
+ {# the JS file lives at "public/bundles/acme/js/loader.js" #}
263
305
<script src="{{ asset('bundles/acme/js/loader.js') }}"></script>
264
306
265
307
The ``asset() `` function's main purpose is to make your application more portable.
@@ -334,10 +376,8 @@ gives you access to these variables:
334
376
A :class: `Symfony\\ Component\\ Security\\ Core\\ Authentication\\ Token\\ TokenInterface `
335
377
object representing the security token.
336
378
337
- .. tip ::
338
-
339
- In addition to the global ``app `` variable injected by Symfony, you can also
340
- :doc: `inject variables automatically to all Twig templates </templating/global_variables >`.
379
+ In addition to the global ``app `` variable injected by Symfony, you can also
380
+ :doc: `inject variables automatically to all Twig templates </templating/global_variables >`.
341
381
342
382
.. _templates-rendering :
343
383
@@ -412,6 +452,8 @@ Rendering a Template in Emails
412
452
413
453
Read the docs about the :ref: `mailer and Twig integration <mailer-twig >`.
414
454
455
+ .. _templates-render-from-route :
456
+
415
457
Rendering a Template Directly from a Route
416
458
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
417
459
@@ -434,7 +476,10 @@ definition. Use the special ``TemplateController`` provided by Symfony::
434
476
# special options defined by Symfony to set the page cache
435
477
maxAge : 86400
436
478
sharedAge : 86400
437
- methods : GET
479
+
480
+ # optionally you can define some arguments passed to the template
481
+ site_name : ' ACME'
482
+ theme : ' dark'
438
483
439
484
.. code-block :: xml
440
485
@@ -446,14 +491,17 @@ definition. Use the special ``TemplateController`` provided by Symfony::
446
491
447
492
<route id =" acme_privacy"
448
493
path =" /privacy"
449
- controller =" Symfony\Bundle\FrameworkBundle\Controller\TemplateController"
450
- methods =" GET" >
494
+ controller =" Symfony\Bundle\FrameworkBundle\Controller\TemplateController" >
451
495
<!-- the path of the template to render -->
452
496
<default key =" template" >static/privacy.html.twig</default >
453
497
454
498
<!-- special options defined by Symfony to set the page cache -->
455
499
<default key =" maxAge" >86400</default >
456
500
<default key =" sharedAge" >86400</default >
501
+
502
+ <!-- optionally you can define some arguments passed to the template -->
503
+ <default key =" site_name" >ACME</default >
504
+ <default key =" theme" >dark</default >
457
505
</route >
458
506
</routes >
459
507
@@ -466,14 +514,17 @@ definition. Use the special ``TemplateController`` provided by Symfony::
466
514
return function (RoutingConfigurator $routes) {
467
515
$routes->add('acme_privacy', '/privacy')
468
516
->controller(TemplateController::class)
469
- ->methods(['GET'])
470
517
->defaults([
471
518
// the path of the template to render
472
519
'template' => 'static/privacy.html.twig',
473
520
474
521
// special options defined by Symfony to set the page cache
475
522
'maxAge' => 86400,
476
523
'sharedAge' => 86400,
524
+
525
+ // optionally you can define some arguments passed to the template
526
+ 'site_name' => 'ACME',
527
+ 'theme' => 'dark',
477
528
])
478
529
;
479
530
};
0 commit comments