mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 02:16:41 +00:00
Merge branch '3.x' into 4.x
* 3.x: Fix wrong array index (again) fix: update extension references in docs to use backticks [Doc] Fix `code-block` in html_cva [Docs] Replace `=` by `:` in code examples fix: deprecated documentation Use `:` instead of `=` for named argument in the docs use EmptyNode instead of an Nodes instance without children Fix `ModuleNode` instanciation when `$embeddedTemplates` is null merge the Nodes and Node sections reduce the number of deprecations being triggered
This commit is contained in:
+7
-7
@@ -325,23 +325,23 @@ extension via the ``addExtension()`` method::
|
||||
|
||||
Twig comes bundled with the following extensions:
|
||||
|
||||
* *Twig\Extension\CoreExtension*: Defines all the core features of Twig.
|
||||
* ``\Twig\Extension\CoreExtension``: Defines all the core features of Twig.
|
||||
|
||||
* *Twig\Extension\DebugExtension*: Defines the ``dump`` function to help debug
|
||||
* ``\Twig\Extension\DebugExtension``: Defines the ``dump`` function to help debug
|
||||
template variables.
|
||||
|
||||
* *Twig\Extension\EscaperExtension*: Adds automatic output-escaping and the
|
||||
* ``\Twig\Extension\EscaperExtension``: Adds automatic output-escaping and the
|
||||
possibility to escape/unescape blocks of code.
|
||||
|
||||
* *Twig\Extension\SandboxExtension*: Adds a sandbox mode to the default Twig
|
||||
* ``\Twig\Extension\SandboxExtension``: Adds a sandbox mode to the default Twig
|
||||
environment, making it safe to evaluate untrusted code.
|
||||
|
||||
* *Twig\Extension\ProfilerExtension*: Enables the built-in Twig profiler.
|
||||
* ``\Twig\Extension\ProfilerExtension``: Enables the built-in Twig profiler.
|
||||
|
||||
* *Twig\Extension\OptimizerExtension*: Optimizes the node tree before
|
||||
* ``\Twig\Extension\OptimizerExtension``: Optimizes the node tree before
|
||||
compilation.
|
||||
|
||||
* *Twig\Extension\StringLoaderExtension*: Defines the ``template_from_string``
|
||||
* ``\Twig\Extension\StringLoaderExtension``: Defines the ``template_from_string``
|
||||
function to allow loading templates from string in a template.
|
||||
|
||||
The Core, Escaper, and Optimizer extensions are registered by default.
|
||||
|
||||
@@ -22,37 +22,37 @@ The list of supported options:
|
||||
* ``grouping_used``: Specifies whether to use grouping separator for thousands::
|
||||
|
||||
{# 1,234,567.89 #}
|
||||
{{ 1234567.89|format_number({grouping_used:true}, locale='en') }}
|
||||
{{ 1234567.89|format_number({grouping_used:true}, locale: 'en') }}
|
||||
|
||||
* ``decimal_always_shown``: Specifies whether to always show the decimal part, even if it's zero::
|
||||
|
||||
{# 123. #}
|
||||
{{ 123|format_number({decimal_always_shown:true}, locale='en') }}
|
||||
{{ 123|format_number({decimal_always_shown:true}, locale: 'en') }}
|
||||
|
||||
* ``max_integer_digit``:
|
||||
* ``min_integer_digit``:
|
||||
* ``integer_digit``: Define constraints on the integer part::
|
||||
|
||||
{# 345.679 #}
|
||||
{{ 12345.6789|format_number({max_integer_digit:3, min_integer_digit:2}, locale='en') }}
|
||||
{{ 12345.6789|format_number({max_integer_digit:3, min_integer_digit:2}, locale: 'en') }}
|
||||
|
||||
* ``max_fraction_digit``:
|
||||
* ``min_fraction_digit``:
|
||||
* ``fraction_digit``: Define constraints on the fraction part::
|
||||
|
||||
{# 123.46 #}
|
||||
{{ 123.456789|format_number({max_fraction_digit:2, min_fraction_digit:1}, locale='en') }}
|
||||
{{ 123.456789|format_number({max_fraction_digit:2, min_fraction_digit:1}, locale: 'en') }}
|
||||
|
||||
* ``multiplier``: Multiplies the value before formatting::
|
||||
|
||||
{# 123,000 #}
|
||||
{{ 123|format_number({multiplier:1000}, locale='en') }}
|
||||
{{ 123|format_number({multiplier:1000}, locale: 'en') }}
|
||||
|
||||
* ``grouping_size``:
|
||||
* ``secondary_grouping_size``: Set the size of the primary and secondary grouping separators::
|
||||
|
||||
{# 1,23,45,678 #}
|
||||
{{ 12345678|format_number({grouping_size:3, secondary_grouping_size:2}, locale='en') }}
|
||||
{{ 12345678|format_number({grouping_size:3, secondary_grouping_size:2}, locale: 'en') }}
|
||||
|
||||
* ``rounding_mode``:
|
||||
* ``rounding_increment``: Control rounding behavior, here is a list of all rounding_mode available:
|
||||
@@ -67,7 +67,7 @@ The list of supported options:
|
||||
.. code-block:: twig
|
||||
|
||||
{# 123.5 #}
|
||||
{{ 123.456|format_number({rounding_mode:'ceiling', rounding_increment:0.05}, locale='en') }}
|
||||
{{ 123.456|format_number({rounding_mode:'ceiling', rounding_increment:0.05}, locale: 'en') }}
|
||||
|
||||
* ``format_width``:
|
||||
* ``padding_position``: Set width and padding for the formatted number, here is a list of all padding_position available:
|
||||
@@ -79,19 +79,19 @@ The list of supported options:
|
||||
.. code-block:: twig
|
||||
|
||||
{# 123 #}
|
||||
{{ 123|format_number({format_width:10, padding_position:'before_suffix'}, locale='en') }}
|
||||
{{ 123|format_number({format_width:10, padding_position:'before_suffix'}, locale: 'en') }}
|
||||
|
||||
* ``significant_digits_used``:
|
||||
* ``min_significant_digits_used``:
|
||||
* ``max_significant_digits_used``: Control significant digits in formatting::
|
||||
|
||||
{# 123.4568 #}
|
||||
{{ 123.456789|format_number({significant_digits_used:true, min_significant_digits_used:4, max_significant_digits_used:7}, locale='en') }}
|
||||
{{ 123.456789|format_number({significant_digits_used:true, min_significant_digits_used:4, max_significant_digits_used:7}, locale: 'en') }}
|
||||
|
||||
* ``lenient_parse``: If true, allows lenient parsing of the input::
|
||||
|
||||
{# 123 #}
|
||||
{{ 123|format_number({lenient_parse:true}, locale='en') }}
|
||||
{{ 123|format_number({lenient_parse:true}, locale: 'en') }}
|
||||
|
||||
Besides plain numbers, the filter can also format numbers in various styles::
|
||||
|
||||
@@ -109,37 +109,37 @@ The list of supported styles:
|
||||
* ``decimal``::
|
||||
|
||||
{# 1,234.568 #}
|
||||
{{ 1234.56789 | format_number(style='decimal', locale='en') }}
|
||||
{{ 1234.56789 | format_number(style: 'decimal', locale: 'en') }}
|
||||
|
||||
* ``currency``::
|
||||
|
||||
{# $1,234.56 #}
|
||||
{{ 1234.56 | format_number(style='currency', locale='en') }}
|
||||
{{ 1234.56 | format_number(style: 'currency', locale: 'en') }}
|
||||
|
||||
* ``percent``::
|
||||
|
||||
{# 12% #}
|
||||
{{ 0.1234 | format_number(style='percent', locale='en') }}
|
||||
{{ 0.1234 | format_number(style: 'percent', locale: 'en') }}
|
||||
|
||||
* ``scientific``::
|
||||
|
||||
{# 1.23456789e+3 #}
|
||||
{{ 1234.56789 | format_number(style='scientific', locale='en') }}
|
||||
{{ 1234.56789 | format_number(style: 'scientific', locale: 'en') }}
|
||||
|
||||
* ``spellout``::
|
||||
|
||||
{# one thousand two hundred thirty-four point five six seven eight nine #}
|
||||
{{ 1234.56789 | format_number(style='spellout', locale='en') }}
|
||||
{{ 1234.56789 | format_number(style: 'spellout', locale: 'en') }}
|
||||
|
||||
* ``ordinal``::
|
||||
|
||||
{# 1st #}
|
||||
{{ 1 | format_number(style='ordinal', locale='en') }}
|
||||
{{ 1 | format_number(style: 'ordinal', locale: 'en') }}
|
||||
|
||||
* ``duration``::
|
||||
|
||||
{# 2:30:00 #}
|
||||
{{ 9000 | format_number(style='duration', locale='en') }}
|
||||
{{ 9000 | format_number(style: 'duration', locale: 'en') }}
|
||||
|
||||
As a shortcut, you can use the ``format_*_number`` filters by replacing ``*``
|
||||
with a style::
|
||||
|
||||
@@ -56,7 +56,7 @@ To "merge" conflicting classes together and keep only the ones you need, use the
|
||||
``tailwind_merge()`` filter from `tales-from-a-dev/twig-tailwind-extra`_
|
||||
with the ``html_cva()`` function:
|
||||
|
||||
.. code-block:: terminal
|
||||
.. code-block:: bash
|
||||
|
||||
$ composer require tales-from-a-dev/twig-tailwind-extra
|
||||
|
||||
|
||||
@@ -27,12 +27,12 @@ You can disable access to the context by setting ``with_context`` to
|
||||
.. code-block:: twig
|
||||
|
||||
{# only the name variable will be accessible #}
|
||||
{{ include('template.html.twig', {name: 'Fabien'}, with_context = false) }}
|
||||
{{ include('template.html.twig', {name: 'Fabien'}, with_context: false) }}
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{# no variables will be accessible #}
|
||||
{{ include('template.html.twig', with_context = false) }}
|
||||
{{ include('template.html.twig', with_context: false) }}
|
||||
|
||||
And if the expression evaluates to a ``\Twig\Template`` or a
|
||||
``\Twig\TemplateWrapper`` instance, Twig will use it directly::
|
||||
@@ -48,7 +48,7 @@ the template does not exist:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ include('sidebar.html.twig', ignore_missing = true) }}
|
||||
{{ include('sidebar.html.twig', ignore_missing: true) }}
|
||||
|
||||
You can also provide a list of templates that are checked for existence before
|
||||
inclusion. The first template that exists will be rendered:
|
||||
|
||||
@@ -79,33 +79,32 @@ class ArrayExpression extends AbstractExpression implements SupportDefinedTestIn
|
||||
}
|
||||
|
||||
$compiler->raw('[');
|
||||
$first = true;
|
||||
$nextIndex = 0;
|
||||
foreach ($this->getKeyValuePairs() as $pair) {
|
||||
if (!$first) {
|
||||
$isSequence = true;
|
||||
foreach ($this->getKeyValuePairs() as $i => $pair) {
|
||||
if (0 !== $i) {
|
||||
$compiler->raw(', ');
|
||||
}
|
||||
$first = false;
|
||||
|
||||
$key = null;
|
||||
if ($pair['key'] instanceof ContextVariable) {
|
||||
$pair['key'] = new StringCastUnary($pair['key'], $pair['key']->getTemplateLine());
|
||||
}
|
||||
if ($pair['key'] instanceof LocalVariable) {
|
||||
} elseif ($pair['key'] instanceof LocalVariable) {
|
||||
$key = $pair['key']->getAttribute('name');
|
||||
$pair['key'] = new ConstantExpression($key, $pair['key']->getTemplateLine());
|
||||
}
|
||||
if ($pair['key'] instanceof ConstantExpression) {
|
||||
} elseif ($pair['key'] instanceof ConstantExpression) {
|
||||
$key = $pair['key']->getAttribute('value');
|
||||
}
|
||||
|
||||
if ($nextIndex !== $key && !$pair['value'] instanceof SpreadUnary) {
|
||||
if ($key !== $i) {
|
||||
$isSequence = false;
|
||||
}
|
||||
|
||||
if (!$isSequence && !$pair['value'] instanceof SpreadUnary) {
|
||||
$compiler
|
||||
->subcompile($pair['key'])
|
||||
->raw(' => ')
|
||||
;
|
||||
}
|
||||
++$nextIndex;
|
||||
|
||||
$compiler->subcompile($pair['value']);
|
||||
}
|
||||
|
||||
@@ -55,6 +55,9 @@ Twig supports array notation
|
||||
{% set trad = {194:'ABC',141:'DEF',100:'GHI',170:'JKL',110:'MNO',111:'PQR'} %}
|
||||
{% set trad2 = {'194':'ABC','141':'DEF','100':'GHI','170':'JKL','110':'MNO','111':'PQR'} %}
|
||||
{{ trad == trad2 ? 'OK' : 'KO' }}
|
||||
{% set trad = {11: 'ABC', 2: 'DEF', 4: 'GHI', 3: 'JKL'} %}
|
||||
{% set trad2 = {'11': 'ABC', '2': 'DEF', '4': 'GHI', '3': 'JKL'} %}
|
||||
{{ trad == trad2 ? 'OK' : 'KO' }}
|
||||
|
||||
{# indexes are kept #}
|
||||
{{ { 1: "first", 0: "second" } == { '1': "first", '0': "second" } ? 'OK' : 'KO' }}
|
||||
@@ -104,6 +107,7 @@ ok
|
||||
ok
|
||||
ok
|
||||
|
||||
OK
|
||||
OK
|
||||
|
||||
OK
|
||||
@@ -151,6 +155,7 @@ ok
|
||||
ok
|
||||
ok
|
||||
|
||||
OK
|
||||
OK
|
||||
|
||||
OK
|
||||
|
||||
Reference in New Issue
Block a user