mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-31 12:37:15 +00:00
7fb5d8ee348459bdeec0a4c2367bda14f26b2411
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
…
Twig, the flexible, fast, and secure template language for PHP
==============================================================
Twig is a template language for PHP, released under the new BSD license (code
and documentation).
Twig uses a syntax similar to the Django and Jinja template languages which
inspired the Twig runtime environment.
Sponsors
--------
.. raw:: html
<a href="https://blackfire.io/docs/introduction?utm_source=twig&utm_medium=github_readme&utm_campaign=logo">
<img src="https://static.blackfire.io/assets/intemporals/logo/png/blackfire-io_secondary_horizontal_transparent.png?1" width="255px" alt="Blackfire.io">
</a>
More Information
----------------
Read the `documentation`_ for more information.
.. _documentation: https://twig.symfony.com/documentation
Description
Languages
PHP
99.9%