twig 的foreach

foreach.twig
{% for key, val in items %}

{% endfor %}

twig 如果别的

if.twig
{% if (a == 0 and b > c) or (a == 1 and d != true) %}

{% else %}

{% endif %}

twig Сount

count.twig
// count array 
{{ array_name|length }}

twig 原始价值

raw_value.twig
// shows full html instead of plain text.
{{ variable|raw }}

twig 翻译

translate.twig
{{ 'Edu-Filter'|t({}, {'context' : 'beg'}) }}

{% trans with {'context': 'beg'} %}
    1 Kurs
{% plural total_courses %}
    {{ total_courses }} Kurse
{% endtrans %}

twig 链接

link.twig
{{ link(title, url, { 'class' : 'class_name' }) }}

twig 变量

variable.twig
{%
  set classes = [
    'menu-item',
    'drop-down',
    is_active ? 'active',
  ]
%}

twig 空白

whitespace-2.twig
// To remove both leading and trailing whitespace on either side of a Twig code block

<span>
  {%- for item in items -%}
    {{ item.content }}
  {%- endfor -%}
</span>

// Output:

<span>example</span>
whitespace.twig
// This tag will remove all whitespace between HTML tags, excluding spaces inside blocks of text or space inside an HTML tag.

{% spaceless %}
<div class="example code">
    <strong>{{ prints_out_the_word_example }}</strong>
</div>
{% endspaceless %}

// Output:

<div class="example code"><strong>example</strong></div>

twig 属性

attributes.html.twig
<div{{ attributes.addClass('my-class') }}>
<div{{ attributes.removeClass('my-class') }}>
<div{{ attributes.setAttribute('id', 'myID') }}>
<div{{ attributes.removeAttribute('id') }}>

{% if attribute.hasClass('myClass') %}
  {# do stuff #}
{% endif %}

{# printing out individual attributes #}
{# and use "without" filter to prevent attributes that have already been printed #}
<div class="{{ attributes.class }} my-custom-class"{{ attributes|without('class') }}>

twig 附上图书馆

attach_library.twig
{# only attach our retro library if this is node 1 #} 
{% if node.id == 1 %}
  {{ attach_library('retro/retro') }}
{% endif %}