Skip to content

Commit 9b85a73

Browse files
committed
Merge branch '2.8' into 3.0
* 2.8: [#6472] Updating description, after change Avoid confusion Added file paths Fixes and rewords Documented the config options of TwigBundle [#6427] Adding a header Tests: Explain how to add or remove data in a collection of forms Document constraint validator alias optional
2 parents 0fb9bb5 + bcd7024 commit 9b85a73

File tree

4 files changed

+247
-13
lines changed

4 files changed

+247
-13
lines changed

book/testing.rst

+45
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,51 @@ their type::
701701
PHP format (it converts the keys with square brackets notation - e.g.
702702
``my_form[subject]`` - to PHP arrays).
703703

704+
Adding and Removing Forms to a Collection
705+
.........................................
706+
707+
If you use a :doc:`Collection of Forms </cookbook/form/form_collections>`,
708+
you can't add fields to an existing form with
709+
``$form['task[tags][0][name]'] = 'foo';``. This results in an error
710+
``Unreachable field "…"`` because ``$form`` can only be used in order to
711+
set values of existing fields. In order to add new fields, you have to
712+
add the values to the raw data array::
713+
714+
// Get the form.
715+
$form = $crawler->filter('button')->form();
716+
717+
// Get the raw values.
718+
$values = $form->getPhpValues();
719+
720+
// Add fields to the raw values.
721+
$values['task']['tag'][0]['name'] = 'foo';
722+
$values['task']['tag'][1]['name'] = 'bar';
723+
724+
// Submit the form with the existing and new values.
725+
$crawler = $this->client->request($form->getMethod(), $form->getUri(), $values,
726+
$form->getPhpFiles());
727+
728+
// The 2 tags have been added to the collection.
729+
$this->assertEquals(2, $crawler->filter('ul.tags > li')->count());
730+
731+
Where ``task[tags][0][name]`` is the name of a field created
732+
with JavaScript.
733+
734+
You can remove an existing field, e.g. a tag::
735+
736+
// Get the values of the form.
737+
$values = $form->getPhpValues();
738+
739+
// Remove the first tag.
740+
unset($values['task']['tags'][0]);
741+
742+
// Submit the data.
743+
$crawler = $client->request($form->getMethod(), $form->getUri(),
744+
$values, $form->getPhpFiles());
745+
746+
// The tag has been removed.
747+
$this->assertEquals(0, $crawler->filter('ul.tags > li')->count());
748+
704749
.. index::
705750
pair: Tests; Configuration
706751

book/translation.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,11 @@ need it::
482482
$request = $event->getRequest();
483483

484484
// some logic to determine the $locale
485-
$request->getSession()->set('_locale', $locale);
485+
$request->setLocale($locale);
486486
}
487487

488-
Read :doc:`/cookbook/session/locale_sticky_session` for more on the topic.
488+
Read :doc:`/cookbook/session/locale_sticky_session` for more information on making
489+
the user's locale "sticky" to their session.
489490

490491
.. note::
491492

cookbook/validation/custom_constraint.rst

+4-11
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Constraint Validators with Dependencies
158158
If your constraint validator has dependencies, such as a database connection,
159159
it will need to be configured as a service in the Dependency Injection
160160
Container. This service must include the ``validator.constraint_validator``
161-
tag and an ``alias`` attribute:
161+
tag and may include an ``alias`` attribute:
162162

163163
.. configuration-block::
164164

@@ -186,21 +186,14 @@ tag and an ``alias`` attribute:
186186
->register('validator.unique.your_validator_name', 'Fully\Qualified\Validator\Class\Name')
187187
->addTag('validator.constraint_validator', array('alias' => 'alias_name'));
188188
189-
Your constraint class should now use this alias to reference the appropriate
190-
validator::
189+
As mentioned above, Symfony will automatically look for a class named after
190+
the constraint, with ``Validator`` appended. You can override this in your constraint class::
191191

192192
public function validatedBy()
193193
{
194-
return 'alias_name';
194+
return 'Fully\Qualified\ConstraintValidator\Class\Name'; // or 'alias_name' if provided
195195
}
196196

197-
As mentioned above, Symfony will automatically look for a class named after
198-
the constraint, with ``Validator`` appended. If your constraint validator
199-
is defined as a service, it's important that you override the
200-
``validatedBy()`` method to return the alias used when defining your service,
201-
otherwise Symfony won't use the constraint validator service, and will
202-
instantiate the class instead, without any dependencies injected.
203-
204197
Class Constraint Validator
205198
~~~~~~~~~~~~~~~~~~~~~~~~~~
206199

reference/configuration/twig.rst

+195
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,76 @@ TwigBundle Configuration ("twig")
117117
Configuration
118118
-------------
119119

120+
auto_reload
121+
~~~~~~~~~~~
122+
123+
**type**: ``boolean`` **default**: ``'%kernel.debug%'``
124+
125+
If ``true``, whenever a template is rendered, Symfony checks first if its source
126+
code has changed since it was compiled. If it has changed, the template is
127+
compiled again automatically.
128+
129+
autoescape_service
130+
~~~~~~~~~~~~~~~~~~
131+
132+
**type**: ``string`` **default**: ``null``
133+
134+
As of Twig 1.17, the escaping strategy applied by default to the template is
135+
determined during compilation time based on the filename of the template. This
136+
means for example that the contents of a ``*.html.twig`` template are escaped
137+
for HTML and the contents of ``*.js.twig`` are escaped for JavaScript.
138+
139+
This option allows to define the Symfony service which will be used to determine
140+
the default escaping applied to the template.
141+
142+
autoescape_service_method
143+
~~~~~~~~~~~~~~~~~~~~~~~~~
144+
145+
**type**: ``string`` **default**: ``null``
146+
147+
If ``autoescape_service`` option is defined, then this option defines the method
148+
called to determine the default escaping applied to the template.
149+
150+
base_template_class
151+
~~~~~~~~~~~~~~~~~~~
152+
153+
**type**: ``string`` **default**: ``'Twig_Template'``
154+
155+
Twig templates are compiled into PHP classes before using them to render
156+
contents. This option defines the base class from which all the template classes
157+
extend. Using a custom base template is discouraged because it will make your
158+
application harder to maintain.
159+
160+
cache
161+
~~~~~
162+
163+
**type**: ``string`` **default**: ``'%kernel.cache_dir%/twig'``
164+
165+
Before using the Twig templates to render some contents, they are compiled into
166+
regular PHP code. Compilation is a costly process, so the result is cached in
167+
the directory defined by this configuration option.
168+
169+
Set this option to ``null`` to disable Twig template compilation. However, this
170+
is not recommended; not even in the ``dev`` environment, because the
171+
``auto_reload`` option ensures that cached templates which have changed get
172+
compiled again.
173+
174+
charset
175+
~~~~~~~
176+
177+
**type**: ``string`` **default**: ``'%kernel.charset%'``
178+
179+
The charset used by the template files. In the Symfony Standard edition this
180+
defaults to the ``UTF-8`` charset.
181+
182+
debug
183+
~~~~~
184+
185+
**type**: ``boolean`` **default**: ``'%kernel.debug%'``
186+
187+
If ``true``, the compiled templates include a ``__toString()`` method that can
188+
be used to display their nodes.
189+
120190
.. _config-twig-exception-controller:
121191

122192
exception_controller
@@ -132,3 +202,128 @@ conditions (see :doc:`/cookbook/controller/error_pages`). Modifying this
132202
option is advanced. If you need to customize an error page you should use
133203
the previous link. If you need to perform some behavior on an exception,
134204
you should add a listener to the ``kernel.exception`` event (see :ref:`dic-tags-kernel-event-listener`).
205+
206+
optimizations
207+
~~~~~~~~~~~~~
208+
209+
**type**: ``int`` **default**: ``-1``
210+
211+
Twig includes an extension called ``optimizer`` which is enabled by default in
212+
Symfony applications. This extension analyzes the templates to optimize them
213+
when being compiled. For example, if your template doesn't use the special
214+
``loop`` variable inside a ``for`` tag, this extension removes the initialization
215+
of that unused variable.
216+
217+
By default, this option is ``-1``, which means that all optimizations are turned
218+
on. Set it to ``0`` to disable all the optimizations. You can even enable or
219+
disable these optimizations selectively, as explained in the Twig documentation
220+
about `the optimizer extension`_.
221+
222+
paths
223+
~~~~~
224+
225+
**type**: ``array`` **default**: ``null``
226+
227+
This option defines the directories where Symfony will look for Twig templates
228+
in addition to the default locations (``app/Resources/views/`` and the bundles'
229+
``Resources/views/`` directories). This is useful to integrate the templates
230+
included in some library or package used by your application.
231+
232+
The values of the ``paths`` option are defined as ``key: value`` pairs where the
233+
``value`` part can be ``null``. For example:
234+
235+
.. configuration-block::
236+
237+
.. code-block:: yaml
238+
239+
# app/config/config.yml
240+
twig:
241+
# ...
242+
paths:
243+
'%kernel.root_dir%/../vendor/acme/foo-bar/templates': ~
244+
245+
.. code-block:: xml
246+
247+
<!-- app/config/config.xml -->
248+
<container xmlns="http://symfony.com/schema/dic/services"
249+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
250+
xmlns:twig="http://symfony.com/schema/dic/twig"
251+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
252+
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
253+
254+
<twig:config>
255+
<!-- ... -->
256+
<twig:path>%kernel.root_dir%/../vendor/acme/foo-bar/templates</twig:path>
257+
</twig:config>
258+
</container>
259+
260+
.. code-block:: php
261+
262+
// app/config/config.php
263+
$container->loadFromExtension('twig', array(
264+
// ...
265+
'paths' => array(
266+
'%kernel.root_dir%/../vendor/acme/foo-bar/templates' => null,
267+
),
268+
));
269+
270+
The directories defined in the ``paths`` option have more priority than the
271+
default directories defined by Symfony. In the above example, if the template
272+
exists in the ``acme/foo-bar/templates/`` directory inside your application's
273+
``vendor/``, it will be used by Symfony.
274+
275+
If you provide a value for any path, Symfony will consider it the Twig namespace
276+
for that directory:
277+
278+
.. configuration-block::
279+
280+
.. code-block:: yaml
281+
282+
# app/config/config.yml
283+
twig:
284+
# ...
285+
paths:
286+
'%kernel.root_dir%/../vendor/acme/foo-bar/templates': 'foo_bar'
287+
288+
.. code-block:: xml
289+
290+
<!-- app/config/config.xml -->
291+
<container xmlns="http://symfony.com/schema/dic/services"
292+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
293+
xmlns:twig="http://symfony.com/schema/dic/twig"
294+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
295+
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
296+
297+
<twig:config>
298+
<!-- ... -->
299+
<twig:path namespace="foo_bar">%kernel.root_dir%/../vendor/acme/foo-bar/templates</twig:path>
300+
</twig:config>
301+
</container>
302+
303+
.. code-block:: php
304+
305+
# app/config/config.php
306+
$container->loadFromExtension('twig', array(
307+
// ...
308+
'paths' => array(
309+
'%kernel.root_dir%/../vendor/acme/foo-bar/templates' => 'foo_bar',
310+
),
311+
));
312+
313+
This option is useful to not mess with the default template directories defined
314+
by Symfony. Besides, it simplifies how you refer to those templates:
315+
316+
.. code-block:: text
317+
318+
@foo_bar/template_name.html.twig
319+
320+
strict_variables
321+
~~~~~~~~~~~~~~~~
322+
323+
**type**: ``boolean`` **default**: ``'%kernel.debug%'``
324+
325+
If set to ``true``, Symfony shows an exception whenever a Twig variable,
326+
attribute or method doesn't exist. If set to ``false`` these errors are ignored
327+
and the non-existing values are replaced by ``null``.
328+
329+
.. _`the optimizer extension`: http://twig.sensiolabs.org/doc/api.html#optimizer-extension

0 commit comments

Comments
 (0)