Commit Graph

3978 Commits

Author SHA1 Message Date
Fabien Potencier 54eff0d96e bug #3341 In |filter(), |map(), and |reduce(), throw a RuntimeError instead of a TypeError (thijzert)
This PR was squashed before being merged into the 1.x branch.

Discussion
----------

In |filter(), |map(), and |reduce(), throw a RuntimeError instead of a TypeError

If a template accidentally performs `|filter(...)` on a variable that isn't traversable (e.g. `null`), the calling code will encouter the raw `TypeError` that results from Twig trying to construct an `IteratorIterator` on that variable. This means that context information (e.g., in which exact template the error occurred, and on which line) is discarded, which makes it very difficult to debug your templates.

This commit adds code to throw a `RuntimeError` if Twig encounters a situation where there's no valid way to filter an array.

Commits
-------

567b1e2a In |filter(), |map(), and |reduce(), throw a RuntimeError instead of a TypeError
2020-08-23 16:40:19 +02:00
Thijs van Dijk 567b1e2a06 In |filter(), |map(), and |reduce(), throw a RuntimeError instead of a TypeError 2020-08-23 16:40:13 +02:00
Fabien Potencier 80a4a1f31e Remove tests that do not make sense 2020-08-23 14:46:38 +02:00
Fabien Potencier df5d4c3aad Update CHANGELOG 2020-08-23 14:20:00 +02:00
Fabien Potencier d14dab78ce Fix previous merge 2020-08-23 14:19:17 +02:00
Fabien Potencier 7fb5d8ee34 feature #3255 Make round brackets optional for one argument tests like sameas (iquito)
This PR was submitted for the 3.x branch but it was squashed and merged into the 1.x branch instead.

Discussion
----------

Make round brackets optional for one argument tests like sameas

Currently Twig forces round brackets when doing tests with `is` if there is an argument:

    {{ 1 is same as(1) ? 'OK' }}
    {{ 8 is divisible by(2) ? 'OK' }}

That syntax always bumped me, because without the brackets it would be easier to understand and write, especially for people who do not know Twig internals:

    {{ 1 is same as 1 ? 'OK' }}
    {{ 8 is divisible by 2 ? 'OK' }}

Twig only has the `same as` and `divisible by` tests which need arguments (in the base library), and for both you usually only need one argument, so it could make the template syntax more concise. [Jinja already supports this syntax](https://jinja.palletsprojects.com/en/2.11.x/templates/#tests) for one argument tests, so it would be one less difference between Jinja and Twig for people who use both.

The changes in this pull request should be fully backwards-compatible - instead of Twig throwing a SyntaxError it now accepts one argument when the round brackets are missing. I added an option for TwigTest to signal that there is one mandatory argument for the test, otherwise the ExpressionParser does not know it should look for that mandatory argument, and this feature is therefore available to anybody writing their own TwigTests, which could help extensions to Twig to simplify their syntax too.

I also adjusted the unit tests for `sameas` and `divisibleby` in hopefully a sensible way.

Commits
-------

1ee72d9e Make round brackets optional for one argument tests like sameas
2020-08-23 14:16:43 +02:00
Andreas 1ee72d9e15 Make round brackets optional for one argument tests like sameas 2020-08-23 14:16:31 +02:00
Fabien Potencier c6927de9e7 Bump version 2020-08-23 13:59:06 +02:00
Fabien Potencier 244ef52df6 Tweak docs 2020-08-23 13:58:27 +02:00
Fabien Potencier b0ce4f64ba Update CHANGELOG 2020-08-23 13:56:44 +02:00
Fabien Potencier 6fe2924795 feature #3277 Support object initialisation shortcut {a} == {a:a} from ES2015 (GromNaN)
This PR was submitted for the 3.x branch but it was merged into the 1.x branch instead.

Discussion
----------

Support object initialisation shortcut {a} == {a:a} from ES2015

Object declaration in Twig is very close to the Javascript syntax and is targeting the same profil of developers. In order to provide a smooth developer experience, Twig should implement some new feature of ECMAScript.

Since ECMAScript 2015, a short syntax can be used to declare an object when the key is identical to a defined variable (see [MDN doc](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer))

```js
// Shorthand property names (ES2015)
let a = 'foo', b = 42, c = {};
let o = {a, b, c}
```

This PR adds the same syntax to Twig.
This can be very useful for `include ... with ... only` :
```twig
{% set title = "My title" %}
{% set image = "image.jpg" %}
{{ include "template.twig" with {title, image} only }}
# instead of
{{ include "template.twig" with {title: title, image: image} only }}
```

Commits
-------

133edb36 Support object init from variable
2020-08-23 13:55:00 +02:00
Jérôme TAMARELLE 133edb3696 Support object init from variable
{a} is a shortcut for {a:a}
From ECMAScript 2015 syntax https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer
2020-08-23 13:54:49 +02:00
Fabien Potencier bde54e11b8 Fix CS 2020-08-23 13:48:32 +02:00
Fabien Potencier 2c7cad3416 minor #3390 More PHPUnit fixes (fabpot)
This PR was squashed before being merged into the 1.x branch.

Discussion
----------

More PHPUnit fixes

Commits
-------

f787c032 Migrate more tests
ad8d012e Update phpunit config
2020-08-23 13:43:11 +02:00
Fabien Potencier f787c032ab Migrate more tests 2020-08-23 12:47:51 +02:00
Fabien Potencier ad8d012e20 Update phpunit config 2020-08-23 12:46:19 +02:00
Fabien Potencier a293147027 minor #3389 Update phpunit assertion (fabpot)
This PR was merged into the 1.x branch.

Discussion
----------

Update phpunit assertion

Commits
-------

e5882d9f Update phpunit assertion
2020-08-23 12:30:49 +02:00
Fabien Potencier e5882d9fb4 Update phpunit assertion 2020-08-23 12:22:59 +02:00
Fabien Potencier 4ba9822987 Bump version to 1.43.2-DEV 2020-08-05 17:06:12 +02:00
Fabien Potencier 2311602f6a Prepare the 1.43.1 release v1.43.1 2020-08-05 17:05:05 +02:00
Fabien Potencier 5383d354d3 Update CHANGELOG 2020-08-05 17:00:10 +02:00
Fabien Potencier 94aa9bf305 Add a test 2020-08-05 16:53:34 +02:00
Fabien Potencier b65facc1c5 minor #3373 fix typo in ApplyTokenParser PHP doc (campino2k)
This PR was submitted for the 3.x branch but it was merged into the 1.x branch instead.

Discussion
----------

fix typo in ApplyTokenParser PHP doc

Typo

Commits
-------

47b5028f fix typo in ApplyTokenParser PHP doc
2020-07-21 13:11:55 +02:00
Chris Jung 47b5028f46 fix typo in ApplyTokenParser PHP doc
Typo
2020-07-21 13:11:49 +02:00
Fabien Potencier cb06013c8d bug #3348 Fixes #3351 (Bozhidar Hristov)
This PR was submitted for the 3.x branch but it was squashed and merged into the 1.x branch instead.

Discussion
----------

Fixes #3351

Sandbox mode is not disabled if syntax error occurs inside {% sandbox %} tag

Fixes #3351

Commits
-------

04658c9f Fixes #3351
2020-07-09 14:43:42 +02:00
Bozhidar Hristov 04658c9f59 Fixes #3351 2020-07-09 14:43:36 +02:00
Fabien Potencier 2ecc15a440 bug #3365 Fix a regression when not using a space before an operator (fabpot)
This PR was merged into the 1.x branch.

Discussion
----------

Fix a regression when not using a space before an operator

Commits
-------

841906b1 Fix a regression when not using a space before an operator
2020-07-06 15:34:54 +02:00
Fabien Potencier b4088fa607 minor #3367 Unify code style (apfelbox)
This PR was merged into the 1.x branch.

Discussion
----------

Unify code style

I noticed that the code style in `ExpressionParser` changed from v1 to v3. So this PR now fixes it, that the file in v1 looks more clean.

@fabpot this is a small addendum to #3363

Commits
-------

38cb89c2 Unify code style
2020-07-06 15:26:54 +02:00
Jannik Zschiesche 38cb89c2bc Unify code style 2020-07-06 14:57:49 +02:00
Fabien Potencier 841906b110 Fix a regression when not using a space before an operator 2020-07-06 14:40:02 +02:00
Fabien Potencier aa182ff7c1 feature #3363 Allow trailing commas in macros + functions + filters (apfelbox)
This PR was squashed before being merged into the 1.x branch.

Discussion
----------

Allow trailing commas in macros + functions + filters

I implemented support for trailing commas in

- function calls
- filter arguments
- macro parameters + arguments (so call + definition)

Lists + maps are already supported https://twigfiddle.com/6atiz3

Is something missing?

Commits
-------

ccc2dbb0 Allow trailing commas in macros + functions + filters
2020-07-06 13:58:13 +02:00
Jannik Zschiesche ccc2dbb043 Allow trailing commas in macros + functions + filters 2020-07-06 13:58:05 +02:00
Fabien Potencier 471e965e9e bug #3308 Restrict callables to closures in filters (jderusse)
This PR was squashed before being merged into the 1.x branch.

Discussion
----------

Restrict callables to closures in filters

When using filter in sandboxed mode, this PR requires the use of closure.

Commits
-------

d8259519 Restrict callables to closures in filters
2020-07-05 15:30:24 +02:00
Jérémy Derussé d8259519c0 Restrict callables to closures in filters 2020-07-05 15:30:15 +02:00
Fabien Potencier 7e08a97e0a Bump version to 1.43.1-DEV 2020-07-05 15:01:55 +02:00
Fabien Potencier 597a03e85a Prepare the 1.43.0 release v1.43.0 2020-07-05 15:00:49 +02:00
Fabien Potencier 71b4ebc929 Update CHANGELOG 2020-07-05 14:59:53 +02:00
Fabien Potencier 636215f87c Add version in the docs to be more search engine friendly 2020-06-22 17:24:49 +02:00
Fabien Potencier 81aec49572 Fix Drupal tests 2020-06-22 09:21:52 +02:00
Fabien Potencier e99363231b Fix CS 2020-06-22 08:11:03 +02:00
Fabien Potencier 3daadc5175 bug #3326 Throw exception in case non-Traversable data is passed to "filter" (Lustmored)
This PR was submitted for the 3.x branch but it was merged into the 1.x branch instead.

Discussion
----------

Throw exception in case non-Traversable data is passed to "filter"

In PHP 7.4 passing for example null to |filter generates TypeError. I was unsure how to proceed from there as filter could silently fall back to empty result, but instead I went with throwing RuntimeExceptions to be consistent with |batch and |column.

I got confused in structure of tests here, so if that should have some kind of test please push me in the right direction and I'll gladly add it.

Commits
-------

5311963e Throw exception in case non-Traversable data is passed to "filter" filter
2020-05-25 07:16:40 +02:00
Jakub Caban 5311963eb3 Throw exception in case non-Traversable data is passed to "filter" filter 2020-05-25 07:16:34 +02:00
Fabien Potencier fa94cc4934 minor #3340 Improve batch code example (y2keeper)
This PR was submitted for the 2.x branch but it was merged into the 1.x branch instead.

Discussion
----------

Improve batch code example

The current example uses an array of 7 items, resulting in 3 arrays of 3 items each (with fill).

This can be misinterpreted at a glance as creating n arrays as opposed to arrays with n items.

We should therefore instead use an example array of 4 items resulting in 2 arrays of 3 items each (with fill).

I think this would make the functionality less ambiguous.

Commits
-------

feb93210 Improve batch code example
2020-05-25 07:15:31 +02:00
y2keeper feb9321022 Improve batch code example
The current example uses an array of 7 items, resulting in 3 arrays of 3 items each (with fill). 

This can be misinterpreted at a glance as creating n arrays as opposed to arrays with n items.

We should therefore instead use an example array of 4 items resulting in 2 arrays of 3 items each (with fill). 

I think this would make the functionality less ambiguous.
2020-05-25 07:15:25 +02:00
Nicolas Grekas 8f0f071681 minor #3338 Minor fixes (nicolas-grekas)
This PR was merged into the 1.x branch.

Discussion
----------

Minor fixes

Commits
-------

5dc36446 Minor fixes
2020-05-21 11:11:52 +02:00
Nicolas Grekas 5dc364469b Minor fixes 2020-05-21 11:01:36 +02:00
Fabien Potencier f59c2596ad Tweak PHP constraint 2020-05-21 07:36:43 +02:00
Fabien Potencier 9b710b9cf0 minor #3336 Remove boilerplate to run tests on PHP 8 (nicolas-grekas)
This PR was merged into the 1.x branch.

Discussion
----------

Remove boilerplate to run tests on PHP 8

Follows https://github.com/symfony/symfony/pull/36882

Commits
-------

dd3defb9 Remove boilerplate to run tests on PHP 8
2020-05-21 07:35:01 +02:00
Nicolas Grekas dd3defb982 Remove boilerplate to run tests on PHP 8 2020-05-20 19:48:43 +02:00
Fabien Potencier 37f953956e minor #3334 Remove deprecated option (dbrumann)
This PR was merged into the 1.x branch.

Discussion
----------

Remove deprecated option

See: https://docs.travis-ci.com/user/reference/overview/#deprecated-virtualization-environments

> If you’re trying to use sudo: false or dist: precise keys in your travis.yml, we recommend you remove them and switch to our current Xenial Linux infrastructure.

Commits
-------

b003d737 Remove deprecated option
2020-05-19 16:00:38 +02:00