mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 20:16:45 +00:00
Remove most usage of foo/bar/baz in the docs
This commit is contained in:
+9
-9
@@ -271,8 +271,8 @@ A dynamic filter can define more than one dynamic parts::
|
||||
|
||||
The filter receives all dynamic part values before the normal filter arguments,
|
||||
but after the environment and the context. For instance, a call to
|
||||
``'foo'|a_path_b()`` will result in the following arguments to be passed to the
|
||||
filter: ``('a', 'b', 'foo')``.
|
||||
``'Paris'|a_path_b()`` will result in the following arguments to be passed to the
|
||||
filter: ``('a', 'b', 'Paris')``.
|
||||
|
||||
Deprecated Filters
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
@@ -322,7 +322,7 @@ template using it. See :ref:`deprecation-notices` for more information.
|
||||
|
||||
$filter = new \Twig\TwigFilter('obsolete', function () {
|
||||
// ...
|
||||
}, ['deprecated' => '1.1', 'deprecating_package' => 'foo/bar']);
|
||||
}, ['deprecated' => '1.1', 'deprecating_package' => 'twig/some-package']);
|
||||
|
||||
Functions
|
||||
---------
|
||||
@@ -924,14 +924,14 @@ structure in your test directory::
|
||||
|
||||
Fixtures/
|
||||
filters/
|
||||
foo.test
|
||||
bar.test
|
||||
lower.test
|
||||
upper.test
|
||||
functions/
|
||||
foo.test
|
||||
bar.test
|
||||
date.test
|
||||
format.test
|
||||
tags/
|
||||
foo.test
|
||||
bar.test
|
||||
for.test
|
||||
if.test
|
||||
IntegrationTest.php
|
||||
|
||||
The ``IntegrationTest.php`` file should look like this::
|
||||
|
||||
+12
-12
@@ -398,14 +398,14 @@ The escaping rules are implemented as follows:
|
||||
|
||||
.. code-block:: html+twig
|
||||
|
||||
{{ foo ? "Twig<br/>" : "<br/>Twig" }} {# won't be escaped #}
|
||||
{{ any_value ? "Twig<br/>" : "<br/>Twig" }} {# won't be escaped #}
|
||||
|
||||
{% set text = "Twig<br/>" %}
|
||||
{{ true ? text : "<br/>Twig" }} {# will be escaped #}
|
||||
{{ false ? text : "<br/>Twig" }} {# won't be escaped #}
|
||||
|
||||
{% set text = "Twig<br/>" %}
|
||||
{{ foo ? text|raw : "<br/>Twig" }} {# won't be escaped #}
|
||||
{{ any_value ? text|raw : "<br/>Twig" }} {# won't be escaped #}
|
||||
|
||||
* Objects with a ``__toString`` method are converted to strings and
|
||||
escaped. You can mark some classes and/or interfaces as being safe for some
|
||||
@@ -413,17 +413,17 @@ The escaping rules are implemented as follows:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
// mark object of class Foo as safe for the HTML strategy
|
||||
$escaper->addSafeClass('Foo', ['html']);
|
||||
// mark objects of class "HtmlGenerator" as safe for the HTML strategy
|
||||
$escaper->addSafeClass('HtmlGenerator', ['html']);
|
||||
|
||||
// mark object of interface Foo as safe for the HTML strategy
|
||||
$escaper->addSafeClass('FooInterface', ['html']);
|
||||
// mark objects of interface "HtmlGeneratorInterface" as safe for the HTML strategy
|
||||
$escaper->addSafeClass('HtmlGeneratorInterface', ['html']);
|
||||
|
||||
// mark object of class Foo as safe for the HTML and JS strategies
|
||||
$escaper->addSafeClass('Foo', ['html', 'js']);
|
||||
// mark objects of class "HtmlGenerator" as safe for the HTML and JS strategies
|
||||
$escaper->addSafeClass('HtmlGenerator', ['html', 'js']);
|
||||
|
||||
// mark object of class Foo as safe for all strategies
|
||||
$escaper->addSafeClass('Foo', ['all']);
|
||||
// mark objects of class "HtmlGenerator" as safe for all strategies
|
||||
$escaper->addSafeClass('HtmlGenerator', ['all']);
|
||||
|
||||
* Escaping is applied before printing, after any other filter is applied:
|
||||
|
||||
@@ -456,8 +456,8 @@ The escaping rules are implemented as follows:
|
||||
|
||||
Note that autoescaping has some limitations as escaping is applied on
|
||||
expressions after evaluation. For instance, when working with
|
||||
concatenation, ``{{ foo|raw ~ bar }}`` won't give the expected result as
|
||||
escaping is applied on the result of the concatenation, not on the
|
||||
concatenation, ``{{ value|raw ~ other }}`` won't give the expected result
|
||||
as escaping is applied on the result of the concatenation, not on the
|
||||
individual variables (so, the ``raw`` filter won't have any effect here).
|
||||
|
||||
Sandbox Extension
|
||||
|
||||
+16
-16
@@ -15,18 +15,18 @@ standards:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ foo }}
|
||||
{{ user }}
|
||||
{# comment #}
|
||||
{% if foo %}{% endif %}
|
||||
{% if user %}{% endif %}
|
||||
|
||||
When using the whitespace control character, do not put any spaces between
|
||||
it and the delimiter:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{- foo -}}
|
||||
{{- user -}}
|
||||
{#- comment -#}
|
||||
{%- if foo -%}{%- endif -%}
|
||||
{%- if user -%}{%- endif -%}
|
||||
|
||||
* Put exactly one space before and after the following operators:
|
||||
comparison operators (``==``, ``!=``, ``<``, ``>``, ``>=``, ``<=``), math
|
||||
@@ -37,8 +37,8 @@ standards:
|
||||
.. code-block:: twig
|
||||
|
||||
{{ 1 + 2 }}
|
||||
{{ foo ~ bar }}
|
||||
{{ true ? true : false }}
|
||||
{{ first_name ~ ' ' ~ last_name }}
|
||||
{{ is_correct ? true : false }}
|
||||
|
||||
* Put exactly one space after the ``:`` sign in mappings and ``,`` in sequences
|
||||
and mappings:
|
||||
@@ -46,7 +46,7 @@ standards:
|
||||
.. code-block:: twig
|
||||
|
||||
{{ [1, 2, 3] }}
|
||||
{{ {'foo': 'bar'} }}
|
||||
{{ {'name': 'Fabien'} }}
|
||||
|
||||
* Do not put any spaces after an opening parenthesis and before a closing
|
||||
parenthesis in expressions:
|
||||
@@ -59,15 +59,15 @@ standards:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ 'foo' }}
|
||||
{{ "foo" }}
|
||||
{{ 'Twig' }}
|
||||
{{ "Twig" }}
|
||||
|
||||
* Do not put any spaces before and after the following operators: ``|``,
|
||||
``.``, ``..``, ``[]``:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ foo|upper|lower }}
|
||||
{{ name|upper|lower }}
|
||||
{{ user.name }}
|
||||
{{ user[name] }}
|
||||
{% for i in 1..12 %}{% endfor %}
|
||||
@@ -77,7 +77,7 @@ standards:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ foo|default('foo') }}
|
||||
{{ name|default('Fabien') }}
|
||||
{{ range(1..10) }}
|
||||
|
||||
* Do not put any spaces before and after the opening and the closing of
|
||||
@@ -85,22 +85,22 @@ standards:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ [1, 2, 3] }}
|
||||
{{ {'foo': 'bar'} }}
|
||||
[1, 2, 3]
|
||||
{'name': 'Fabien'}
|
||||
|
||||
* Use lower cased and underscored variable names:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set foo = 'foo' %}
|
||||
{% set foo_bar = 'foo' %}
|
||||
{% set name = 'Fabien' %}
|
||||
{% set first_name = 'Fabien' %}
|
||||
|
||||
* Indent your code inside tags (use the same indentation as the one used for
|
||||
the target language of the rendered template):
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% block foo %}
|
||||
{% block content %}
|
||||
{% if true %}
|
||||
true
|
||||
{% endif %}
|
||||
|
||||
+4
-4
@@ -207,12 +207,12 @@ Sandbox
|
||||
Before::
|
||||
|
||||
{% sandbox %}
|
||||
{% include 'foo.twig' %}
|
||||
{% include 'user_defined.twig' %}
|
||||
{% endsandbox %}
|
||||
|
||||
After::
|
||||
|
||||
{{ include('foo.twig', sandboxed: true) }}
|
||||
{{ include('user_defined.twig', sandboxed: true) }}
|
||||
|
||||
Testing Utilities
|
||||
-----------------
|
||||
@@ -260,12 +260,12 @@ Functions/Filters/Tests
|
||||
|
||||
Before::
|
||||
|
||||
$twig->addFunction(new TwigFunction('foo', 'foo', [
|
||||
$twig->addFunction(new TwigFunction('upper', 'upper', [
|
||||
'deprecated' => '3.12', 'deprecating_package' => 'twig/twig',
|
||||
]));
|
||||
|
||||
After::
|
||||
|
||||
$twig->addFunction(new TwigFunction('foo', 'foo', [
|
||||
$twig->addFunction(new TwigFunction('upper', 'upper', [
|
||||
'deprecation_info' => new DeprecatedCallableInfo('twig/twig', '3.12'),
|
||||
]));
|
||||
|
||||
@@ -8,9 +8,9 @@ undefined or empty, otherwise the value of the variable:
|
||||
|
||||
{{ var|default('var is not defined') }}
|
||||
|
||||
{{ var.foo|default('foo item on var is not defined') }}
|
||||
{{ user.name|default('name item on user is not defined') }}
|
||||
|
||||
{{ var['foo']|default('foo item on var is not defined') }}
|
||||
{{ user['name']|default('name item on user is not defined') }}
|
||||
|
||||
{{ ''|default('passed var is empty') }}
|
||||
|
||||
@@ -20,16 +20,17 @@ undefined:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ var.method(foo|default('foo'))|default('foo') }}
|
||||
{{ user.value(name|default('username'))|default('not defined') }}
|
||||
|
||||
Using the ``default`` filter on a boolean variable might trigger unexpected behavior, as
|
||||
``false`` is treated as an empty value. Consider using ``??`` instead:
|
||||
Using the ``default`` filter on a boolean variable might trigger unexpected
|
||||
behavior, as ``false`` is treated as an empty value. Consider using ``??``
|
||||
instead:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set foo = false %}
|
||||
{{ foo|default(true) }} {# true #}
|
||||
{{ foo ?? true }} {# false #}
|
||||
{% set value = false %}
|
||||
{{ value|default(true) }} {# true #}
|
||||
{{ value ?? true }} {# false #}
|
||||
|
||||
.. note::
|
||||
|
||||
|
||||
@@ -120,7 +120,7 @@ callable that accepts a string to escape and the charset::
|
||||
$twig->getRuntime(EscaperRuntime::class)->setEscaper('identity', $escaper);
|
||||
|
||||
# Usage in a template:
|
||||
# {{ 'foo'|escape('identity') }}
|
||||
# {{ 'Twig'|escape('identity') }}
|
||||
|
||||
.. note::
|
||||
|
||||
|
||||
@@ -6,10 +6,10 @@ The ``format`` filter formats a given string by replacing the placeholders
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ "I like %s and %s."|format(foo, "bar") }}
|
||||
{% set fruit = 'apples' %}
|
||||
{{ "I like %s and %s."|format(fruit, "oranges") }}
|
||||
|
||||
{# outputs I like foo and bar
|
||||
if the foo parameter equals to the foo string. #}
|
||||
{# outputs I like apples and oranges #}
|
||||
|
||||
.. seealso::
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ of strings:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set foo = "one,two,three"|split(',') %}
|
||||
{# foo contains ['one', 'two', 'three'] #}
|
||||
{% set items = "one,two,three"|split(',') %}
|
||||
{# items contains ['one', 'two', 'three'] #}
|
||||
|
||||
You can also pass a ``limit`` argument:
|
||||
|
||||
@@ -21,19 +21,19 @@ You can also pass a ``limit`` argument:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set foo = "one,two,three,four,five"|split(',', 3) %}
|
||||
{# foo contains ['one', 'two', 'three,four,five'] #}
|
||||
{% set items = "one,two,three,four,five"|split(',', 3) %}
|
||||
{# items contains ['one', 'two', 'three,four,five'] #}
|
||||
|
||||
If the ``delimiter`` is an empty string, then value will be split by equal
|
||||
chunks. Length is set by the ``limit`` argument (one character by default).
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set foo = "123"|split('') %}
|
||||
{# foo contains ['1', '2', '3'] #}
|
||||
{% set items = "123"|split('') %}
|
||||
{# items contains ['1', '2', '3'] #}
|
||||
|
||||
{% set bar = "aabbcc"|split('', 2) %}
|
||||
{# bar contains ['aa', 'bb', 'cc'] #}
|
||||
{% set items = "aabbcc"|split('', 2) %}
|
||||
{# items contains ['aa', 'bb', 'cc'] #}
|
||||
|
||||
.. note::
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@ mapping as query string:
|
||||
{{ "string with spaces"|url_encode }}
|
||||
{# outputs "string%20with%20spaces" #}
|
||||
|
||||
{{ {'param': 'value', 'foo': 'bar'}|url_encode }}
|
||||
{# outputs "param=value&foo=bar" #}
|
||||
{{ {'name': 'Fabien', 'city': 'Paris'}|url_encode }}
|
||||
{# outputs "name=Fabien&city=Paris" #}
|
||||
|
||||
.. note::
|
||||
|
||||
|
||||
@@ -19,15 +19,15 @@ additional variables:
|
||||
.. code-block:: twig
|
||||
|
||||
{# template.html will have access to the variables from the current context and the additional ones provided #}
|
||||
{{ include('template.html', {foo: 'bar'}) }}
|
||||
{{ include('template.html', {name: 'Fabien'}) }}
|
||||
|
||||
You can disable access to the context by setting ``with_context`` to
|
||||
``false``:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{# only the foo variable will be accessible #}
|
||||
{{ include('template.html', {foo: 'bar'}, with_context = false) }}
|
||||
{# only the name variable will be accessible #}
|
||||
{{ include('template.html', {name: 'Fabien'}, with_context = false) }}
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
|
||||
+8
-8
@@ -47,7 +47,7 @@ named "content":
|
||||
│ │
|
||||
└─────────────────────────────────────┘
|
||||
|
||||
Some pages ("foo" and "bar") share the same content structure -
|
||||
Some pages ("page_1" and "page_2") share the same content structure -
|
||||
two vertically stacked boxes:
|
||||
|
||||
.. code-block:: text
|
||||
@@ -65,7 +65,7 @@ two vertically stacked boxes:
|
||||
│ │
|
||||
└─────────────────────────────────────┘
|
||||
|
||||
While other pages ("boom" and "baz") share a different content structure -
|
||||
While other pages ("page_3" and "page_4") share a different content structure -
|
||||
two boxes side by side:
|
||||
|
||||
.. code-block:: text
|
||||
@@ -86,9 +86,9 @@ two boxes side by side:
|
||||
Without the ``embed`` tag, you have two ways to design your templates:
|
||||
|
||||
* Create two "intermediate" base templates that extend the master layout
|
||||
template: one with vertically stacked boxes to be used by the "foo" and
|
||||
"bar" pages and another one with side-by-side boxes for the "boom" and
|
||||
"baz" pages.
|
||||
template: one with vertically stacked boxes to be used by the "page_1" and
|
||||
"page_2" pages and another one with side-by-side boxes for the "page_3" and
|
||||
"page_4" pages.
|
||||
|
||||
* Embed the markup for the top/bottom and left/right boxes into each page
|
||||
template directly.
|
||||
@@ -111,7 +111,7 @@ code can live in a single base template, and the two different content structure
|
||||
let's call them "micro layouts" go into separate templates which are embedded
|
||||
as necessary:
|
||||
|
||||
Page template ``foo.twig``:
|
||||
Page template ``page_1.twig``:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
@@ -152,11 +152,11 @@ The ``embed`` tag takes the exact same arguments as the ``include`` tag:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% embed "base" with {'foo': 'bar'} %}
|
||||
{% embed "base" with {'name': 'Fabien'} %}
|
||||
...
|
||||
{% endembed %}
|
||||
|
||||
{% embed "base" with {'foo': 'bar'} only %}
|
||||
{% embed "base" with {'name': 'Fabien'} only %}
|
||||
...
|
||||
{% endembed %}
|
||||
|
||||
|
||||
@@ -50,17 +50,17 @@ You can add additional variables by passing them after the ``with`` keyword:
|
||||
.. code-block:: twig
|
||||
|
||||
{# template.html will have access to the variables from the current context and the additional ones provided #}
|
||||
{% include 'template.html' with {'foo': 'bar'} %}
|
||||
{% include 'template.html' with {'name': 'Fabien'} %}
|
||||
|
||||
{% set vars = {'foo': 'bar'} %}
|
||||
{% set vars = {'name': 'Fabien'} %}
|
||||
{% include 'template.html' with vars %}
|
||||
|
||||
You can disable access to the context by appending the ``only`` keyword:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{# only the foo variable will be accessible #}
|
||||
{% include 'template.html' with {'foo': 'bar'} only %}
|
||||
{# only the name variable will be accessible #}
|
||||
{% include 'template.html' with {'name': 'Fabien'} only %}
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
@@ -96,7 +96,7 @@ placed just after the template name. Here some valid examples:
|
||||
.. code-block:: twig
|
||||
|
||||
{% include 'sidebar.html' ignore missing %}
|
||||
{% include 'sidebar.html' ignore missing with {'foo': 'bar'} %}
|
||||
{% include 'sidebar.html' ignore missing with {'name': 'Fabien'} %}
|
||||
{% include 'sidebar.html' ignore missing only %}
|
||||
|
||||
You can also provide a list of templates that are checked for existence before
|
||||
|
||||
+20
-20
@@ -4,45 +4,45 @@
|
||||
Inside code blocks you can also assign values to variables. Assignments use
|
||||
the ``set`` tag and can have multiple targets.
|
||||
|
||||
Here is how you can assign the ``bar`` value to the ``foo`` variable:
|
||||
Here is how you can assign the ``Fabien`` value to the ``name`` variable:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set foo = 'bar' %}
|
||||
{% set name = 'Fabien' %}
|
||||
|
||||
After the ``set`` call, the ``foo`` variable is available in the template like
|
||||
After the ``set`` call, the ``name`` variable is available in the template like
|
||||
any other ones:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{# displays bar #}
|
||||
{{ foo }}
|
||||
{# displays Fabien #}
|
||||
{{ name }}
|
||||
|
||||
The assigned value can be any valid :ref:`Twig expression
|
||||
<twig-expressions>`:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set foo = [1, 2] %}
|
||||
{% set foo = {'foo': 'bar'} %}
|
||||
{% set foo = 'foo' ~ 'bar' %}
|
||||
{% set numbers = [1, 2] %}
|
||||
{% set user = {'name': 'Fabien'} %}
|
||||
{% set name = 'Fabien' ~ ' ' ~ 'Potencier' %}
|
||||
|
||||
Several variables can be assigned in one block:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set foo, bar = 'foo', 'bar' %}
|
||||
{% set first, last = 'Fabien', 'Potencier' %}
|
||||
|
||||
{# is equivalent to #}
|
||||
|
||||
{% set foo = 'foo' %}
|
||||
{% set bar = 'bar' %}
|
||||
{% set first = 'Fabien' %}
|
||||
{% set last = 'Potencier' %}
|
||||
|
||||
The ``set`` tag can also be used to 'capture' chunks of text:
|
||||
The ``set`` tag can also be used to "capture" chunks of text:
|
||||
|
||||
.. code-block:: html+twig
|
||||
|
||||
{% set foo %}
|
||||
{% set content %}
|
||||
<div id="pagination">
|
||||
...
|
||||
</div>
|
||||
@@ -60,19 +60,19 @@ The ``set`` tag can also be used to 'capture' chunks of text:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% for item in list %}
|
||||
{% set foo = item %}
|
||||
{% for item in items %}
|
||||
{% set value = item %}
|
||||
{% endfor %}
|
||||
|
||||
{# foo is NOT available #}
|
||||
{# value is NOT available #}
|
||||
|
||||
If you want to access the variable, just declare it before the loop:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set foo = "" %}
|
||||
{% for item in list %}
|
||||
{% set foo = item %}
|
||||
{% set value = "" %}
|
||||
{% for item in items %}
|
||||
{% set value = item %}
|
||||
{% endfor %}
|
||||
|
||||
{# foo is available #}
|
||||
{# value is available #}
|
||||
|
||||
+6
-5
@@ -9,13 +9,14 @@ The ``types`` tag declares the types of template variables.
|
||||
|
||||
To do this, specify a :ref:`mapping <twig-expressions>` of names to their types as strings.
|
||||
|
||||
Here is how to declare that ``foo`` is a boolean, while ``bar`` is an integer (see note below):
|
||||
Here is how to declare that ``is_correct`` is a boolean, while ``score`` is an
|
||||
integer (see note below):
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% types {
|
||||
foo: 'bool',
|
||||
bar: 'int',
|
||||
is_correct: 'bool',
|
||||
score: 'int',
|
||||
} %}
|
||||
|
||||
You can declare variables as optional by adding the ``?`` suffix:
|
||||
@@ -23,8 +24,8 @@ You can declare variables as optional by adding the ``?`` suffix:
|
||||
.. code-block:: twig
|
||||
|
||||
{% types {
|
||||
foo: 'bool',
|
||||
bar?: 'int',
|
||||
is_correct: 'bool',
|
||||
score?: 'int',
|
||||
} %}
|
||||
|
||||
By default, this tag does not affect the template compilation or runtime behavior.
|
||||
|
||||
+11
-11
@@ -7,10 +7,10 @@ scope are not visible outside of the scope:
|
||||
.. code-block:: twig
|
||||
|
||||
{% with %}
|
||||
{% set foo = 42 %}
|
||||
{{ foo }} {# foo is 42 here #}
|
||||
{% set value = 42 %}
|
||||
{{ value }} {# value is 42 here #}
|
||||
{% endwith %}
|
||||
foo is not visible here any longer
|
||||
value is not visible here any longer
|
||||
|
||||
Instead of defining variables at the beginning of the scope, you can pass a
|
||||
mapping of variables you want to define in the ``with`` tag; the previous
|
||||
@@ -18,13 +18,13 @@ example is equivalent to the following one:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% with {foo: 42} %}
|
||||
{{ foo }} {# foo is 42 here #}
|
||||
{% with {value: 42} %}
|
||||
{{ value }} {# value is 42 here #}
|
||||
{% endwith %}
|
||||
foo is not visible here any longer
|
||||
value is not visible here any longer
|
||||
|
||||
{# it works with any expression that resolves to a mapping #}
|
||||
{% set vars = {foo: 42} %}
|
||||
{% set vars = {value: 42} %}
|
||||
{% with vars %}
|
||||
...
|
||||
{% endwith %}
|
||||
@@ -34,8 +34,8 @@ disable this behavior by appending the ``only`` keyword:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set bar = 'bar' %}
|
||||
{% with {foo: 42} only %}
|
||||
{# only foo is defined #}
|
||||
{# bar is not defined #}
|
||||
{% set zero = 0 %}
|
||||
{% with {value: 42} only %}
|
||||
{# only value is defined #}
|
||||
{# zero is not defined #}
|
||||
{% endwith %}
|
||||
|
||||
+23
-23
@@ -115,9 +115,9 @@ You can assign values to variables inside code blocks. Assignments use the
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set foo = 'foo' %}
|
||||
{% set foo = [1, 2] %}
|
||||
{% set foo = {'foo': 'bar'} %}
|
||||
{% set name = 'Fabien' %}
|
||||
{% set numbers = [1, 2] %}
|
||||
{% set map = {'city': 'Paris'} %}
|
||||
|
||||
Filters
|
||||
-------
|
||||
@@ -516,31 +516,31 @@ exist:
|
||||
writing the number down. If a dot is present the number is a float,
|
||||
otherwise an integer.
|
||||
|
||||
* ``["foo", "bar"]``: Sequences are defined by a sequence of expressions
|
||||
* ``["first_name", "last_name"]``: Sequences are defined by a sequence of expressions
|
||||
separated by a comma (``,``) and wrapped with squared brackets (``[]``).
|
||||
|
||||
* ``{"foo": "bar"}``: Mappings are defined by a list of keys and values
|
||||
* ``{"name": "Fabien"}``: Mappings are defined by a list of keys and values
|
||||
separated by a comma (``,``) and wrapped with curly braces (``{}``):
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{# keys as string #}
|
||||
{'foo': 'foo', 'bar': 'bar'}
|
||||
{'name': 'Fabien', 'city': 'Paris'}
|
||||
|
||||
{# keys as names (equivalent to the previous mapping) #}
|
||||
{foo: 'foo', bar: 'bar'}
|
||||
{name: 'Fabien', city: 'Paris'}
|
||||
|
||||
{# keys as integer #}
|
||||
{2: 'foo', 4: 'bar'}
|
||||
{2: 'Twig', 4: 'Symfony'}
|
||||
|
||||
{# keys can be omitted if it is the same as the variable name #}
|
||||
{foo}
|
||||
{Paris}
|
||||
{# is equivalent to the following #}
|
||||
{'foo': foo}
|
||||
{'Paris': Paris}
|
||||
|
||||
{# keys as expressions (the expression must be enclosed into parentheses) #}
|
||||
{% set foo = 'foo' %}
|
||||
{(foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz'}
|
||||
{% set key = 'name' %}
|
||||
{(key): 'Fabien', (1 + 1): 2, ('ci' ~ 'ty'): 'city'}
|
||||
|
||||
* ``true`` / ``false``: ``true`` represents the true value, ``false``
|
||||
represents the false value.
|
||||
@@ -552,7 +552,7 @@ Sequences and mappings can be nested:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% set foo = [1, {"foo": "bar"}] %}
|
||||
{% set complex = [1, {"name": "Fabien"}] %}
|
||||
|
||||
.. tip::
|
||||
|
||||
@@ -571,8 +571,8 @@ inserted into the string:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ "foo #{bar} baz" }}
|
||||
{{ "foo #{1 + 2} baz" }}
|
||||
{{ "first #{middle} last" }}
|
||||
{{ "first #{1 + 2} last" }}
|
||||
|
||||
.. tip::
|
||||
|
||||
@@ -581,8 +581,8 @@ inserted into the string:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{# outputs foo #{1 + 2} baz #}
|
||||
{{ "foo \#{1 + 2} baz" }}
|
||||
{# outputs first #{1 + 2} last #}
|
||||
{{ "first \#{1 + 2} last" }}
|
||||
|
||||
Math
|
||||
~~~~
|
||||
@@ -821,16 +821,16 @@ The following operators don't fit into any of the other categories:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ foo ? 'yes' : 'no' }}
|
||||
{{ foo ?: 'no' }} is the same as {{ foo ? foo : 'no' }}
|
||||
{{ foo ? 'yes' }} is the same as {{ foo ? 'yes' : '' }}
|
||||
{{ result ? 'yes' : 'no' }}
|
||||
{{ result ?: 'no' }} is the same as {{ result ? result : 'no' }}
|
||||
{{ result ? 'yes' }} is the same as {{ result ? 'yes' : '' }}
|
||||
|
||||
* ``??``: The null-coalescing operator:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{# returns the value of foo if it is defined and not null, 'no' otherwise #}
|
||||
{{ foo ?? 'no' }}
|
||||
{# returns the value of result if it is defined and not null, 'no' otherwise #}
|
||||
{{ result ?? 'no' }}
|
||||
|
||||
* ``...``: The spread operator can be used to expand sequences or mappings or
|
||||
to expand the arguments of a function call:
|
||||
@@ -838,7 +838,7 @@ The following operators don't fit into any of the other categories:
|
||||
.. code-block:: twig
|
||||
|
||||
{% set numbers = [1, 2, ...moreNumbers] %}
|
||||
{% set ratings = {'foo': 10, 'bar': 5, ...moreRatings} %}
|
||||
{% set ratings = {'q1': 10, 'q2': 5, ...moreRatings} %}
|
||||
|
||||
{{ 'Hello %s %s!'|format(...['Fabien', 'Potencier']) }}
|
||||
|
||||
|
||||
@@ -7,16 +7,16 @@ useful if you use the ``strict_variables`` option:
|
||||
.. code-block:: twig
|
||||
|
||||
{# defined works with variable names #}
|
||||
{% if foo is defined %}
|
||||
{% if user is defined %}
|
||||
...
|
||||
{% endif %}
|
||||
|
||||
{# and attributes on variables names #}
|
||||
{% if foo.bar is defined %}
|
||||
{% if user.name is defined %}
|
||||
...
|
||||
{% endif %}
|
||||
|
||||
{% if foo['bar'] is defined %}
|
||||
{% if user['name'] is defined %}
|
||||
...
|
||||
{% endif %}
|
||||
|
||||
@@ -25,6 +25,6 @@ method calls, be sure that they are all defined first:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% if var is defined and foo.method(var) is defined %}
|
||||
{% if var is defined and user.name(var) is defined %}
|
||||
...
|
||||
{% endif %}
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ it will check if an empty string is returned.
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% if foo is empty %}
|
||||
{% if user is empty %}
|
||||
...
|
||||
{% endif %}
|
||||
|
||||
|
||||
@@ -6,6 +6,6 @@ This is equivalent to ``===`` in PHP:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% if foo.attribute is same as(false) %}
|
||||
the foo attribute really is the 'false' PHP value
|
||||
{% if user.name is same as(false) %}
|
||||
the user attribute is the 'false' PHP value
|
||||
{% endif %}
|
||||
|
||||
Reference in New Issue
Block a user