Skip to content

Commit bcd7024

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: [#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 fb7d583 + 373ef72 commit bcd7024

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
@@ -708,6 +708,51 @@ their type::
708708
PHP format (it converts the keys with square brackets notation - e.g.
709709
``my_form[subject]`` - to PHP arrays).
710710

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

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
@@ -167,7 +167,7 @@ Constraint Validators with Dependencies
167167
If your constraint validator has dependencies, such as a database connection,
168168
it will need to be configured as a service in the Dependency Injection
169169
Container. This service must include the ``validator.constraint_validator``
170-
tag and an ``alias`` attribute:
170+
tag and may include an ``alias`` attribute:
171171

172172
.. configuration-block::
173173

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

201201
public function validatedBy()
202202
{
203-
return 'alias_name';
203+
return 'Fully\Qualified\ConstraintValidator\Class\Name'; // or 'alias_name' if provided
204204
}
205205

206-
As mentioned above, Symfony will automatically look for a class named after
207-
the constraint, with ``Validator`` appended. If your constraint validator
208-
is defined as a service, it's important that you override the
209-
``validatedBy()`` method to return the alias used when defining your service,
210-
otherwise Symfony won't use the constraint validator service, and will
211-
instantiate the class instead, without any dependencies injected.
212-
213206
Class Constraint Validator
214207
~~~~~~~~~~~~~~~~~~~~~~~~~~
215208

reference/configuration/twig.rst

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

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

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

0 commit comments

Comments
 (0)