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
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
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
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
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
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
-------
04658c9fFixes#3351
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
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
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
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
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
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
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.
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
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