Skip to content

Commit af36459

Browse files
committed
updated Quick Tour for PR3
1 parent 58dacfb commit af36459

10 files changed

+307
-120
lines changed

glossary.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
:orphan:
2+
13
Glossary
24
========
35

guides/Twig.rst renamed to guides/templating/Twig.rst

+85-18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Installation & Configuration
1717

1818
Enable the ``TwigBundle`` in your kernel::
1919

20+
// hello/config/HelloKernel.php
21+
2022
public function registerBundles()
2123
{
2224
$bundles = array(
@@ -72,6 +74,8 @@ To render a Twig template instead of a PHP one, add the ``:twig`` suffix at the
7274
end of the template name. The controller below renders the ``index.twig``
7375
template::
7476

77+
// src/Application/HelloBundle/Controller/HelloController.php
78+
7579
public function indexAction($name)
7680
{
7781
return $this->render('HelloBundle:Hello:index:twig', array('name' => $name));
@@ -83,38 +87,101 @@ Symfony2 automatically switches the default engine to Twig:
8387

8488
.. code-block:: jinja
8589
86-
{# index.twig #}
90+
{# src/Application/HelloBundle/Resources/views/Hello/index.twig #}
8791
8892
{# 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
90103
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>
93114
94-
{# use the special render tag to render a template #}
95-
{% render 'HelloBundle:Hello:sidebar' %}
96-
{% endblock %}
115+
Include other Templates
116+
-----------------------
97117

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:
100122

101123
.. code-block:: jinja
102124
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:
104140
105-
{% render 'HelloBundle:Hello:sidebar:php' %}
141+
.. code-block:: jinja
106142
107-
And the opposite is also true::
143+
{# index.twig #}
108144
109-
// index.php
145+
{% render 'HelloBundle:Hello:sidebar:php' %}
110146
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+
}
112179

113180
.. index::
114181
single: Twig; Helpers
115182

116-
Helpers
117-
-------
183+
Template Helpers
184+
----------------
118185

119186
The default Symfony2 helpers are available within a Twig template via
120187
specialized tags:
@@ -144,13 +211,13 @@ specialized tags:
144211
{# embed another controller response #}
145212
{% render 'BlogBundle:Post:list' with ['path': ['limit': 2], 'alt': 'BlogBundle:Post:error'] %}
146213
147-
.. _twig_extensions:
214+
.. _twig_extension_tag:
148215

149216
Enabling Custom Twig Extensions
150217
-------------------------------
151218

152219
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``:
154221

155222
.. configuration-block::
156223

index.rst

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Quick Tour
66

77
Get started fast with the Symfony2 :doc:`Quick Tour <quick_tour/index>`:
88

9+
.. toctree::
10+
:hidden:
11+
12+
quick_tour/index_twig
13+
914
.. toctree::
1015
:hidden:
1116

quick_tour/index.rst

-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ Get started fast with the Symfony2 Quick Tour:
1212
the_view
1313
the_controller
1414
the_architecture
15-

quick_tour/index_twig.rst

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Quick Tour (with Twig)
2+
======================
3+
4+
Get started fast with the Symfony2 Quick Tour:
5+
6+
.. toctree::
7+
:maxdepth: 2
8+
:glob:
9+
:numbered:
10+
11+
the_big_picture
12+
the_view_with_twig
13+
the_controller
14+
the_architecture

0 commit comments

Comments
 (0)