着色器 树枝生长
If you’ve ever used Twig before you probably already know about filters and chances are you use them quite often. Filters are very powerful and allow you to modify a variable in an easy yet elegant way. There are lots of filters available and it’s possible that you don’t know all of them yet.
如果您曾经使用过Twig,可能已经对过滤器有所了解,那么您很有可能会经常使用它们。 过滤器非常强大,可让您以简单而优雅的方式修改变量。 有很多可用的过滤器,可能您还不了解所有过滤器。
That’s exactly why we’ll go over this list of filters in this article. But before we continue, I want you to know that you can play around with these filters on twigfiddle.com. This website provides a small online playground where you can fiddle around with Twig. This saves you the hassle of setting up an environment on your local machine.
这就是为什么我们将在本文中介绍此过滤器列表的原因。 但是,在继续之前,我希望您知道可以在twigfiddle.com上使用这些过滤器。 该网站提供了一个小的在线游乐场,您可以在其中闲逛Twig。 这省去了在本地计算机上设置环境的麻烦。
Without further ado, let’s jump straight into the 10 Twig filters you should know.
事不宜迟,让我们直接进入您应该知道的10个Twig过滤器。
1.默认 (1. default)
The default filter returns a specified default value that will be returned if the value is undefined or empty, otherwise the value of the variable gets returned. This filter works on variables but also on objects and arrays.
默认过滤器返回指定的默认值,如果该值未定义或为空,则将返回该默认值,否则将返回该变量的值。 该过滤器不仅适用于变量,而且适用于对象和数组。
{{ name|default('No name defined') }}
{{ person.age|default('Age is not defined') }}
{{ person['age']|default('Age is not defined') }}
2. nl2br (2. nl2br)
nl2br
stands for new line to br which means that this filter inserts HTML line breaks before all newlines in a string. All new lines (\n
) get replaced by an HTML <br>
tag.
nl2br
代表br的换行符,这意味着该过滤器在字符串中的所有换行符之前插入HTML换行符。 所有新行( \n
)都将替换为HTML <br>
标记。
{{ "Hey, how are you?\nJust fine."|nl2br }}{# outputs Hey, how are you?<br /> Just fine.#}
3.未加工 (3. raw)
By default, everything in Twig gets escaped when automatic escaping is enabled. If you don’t want to escape a variable you’ll have to explicitly mark it as safe which you can do by using the raw filter. This only works if the raw filter is the last filter that is applied to the filter.
默认情况下,启用自动转义后,Twig中的所有内容都会转义。 如果您不想转义变量,则必须将其显式标记为安全,这可以通过使用原始过滤器来实现。 仅当原始过滤器是应用于该过滤器的最后一个过滤器时,此方法才有效。
This filter can be useful when you want to render a variable that contains HTML, for example. In that case, you don’t want to escape the variable but rather render it as is.
例如,当您要呈现一个包含HTML的变量时,此过滤器会很有用。 在这种情况下,您不想转义变量,而是按原样呈现它。
{{ data|raw }} {# The data variable won't be escaped #}
4.日期 (4. date)
The date filter formats a date variable to a given format. You can pass in the desired format as the first parameter. The second parameter is optional and gets used to specify the timezone.
日期过滤器将日期变量格式化为给定格式。 您可以将所需的格式作为第一个参数传递。 第二个参数是可选的,用于指定时区。
{{ updated_at|date("d/m/Y") }}
{{ "now"|date("d/m/Y") }}
{{ updated_at|date("d/m/Y", "Europe/Amsterdam") }}
5.批处理 (5. batch)
The batch filter splits up a list in a list of lists — each sublist being called a batch. The first argument specifies the size of a batch. The second argument is a placeholder that gets used to fill in missing items.
批处理过滤器在列表列表中拆分一个列表-每个子列表都称为batch 。 第一个参数指定批处理的大小。 第二个参数是一个占位符,用于填充缺少的项目。
{% set names = ['John', 'Lisa', 'Frank', 'Ted'] %}
<table>
{% for row in names|batch(3, 'No name') %}
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
This is what it will look like once it gets rendered:
呈现后的外观如下所示:
<table>
<tr>
<td>John</td>
<td>Lisa</td>
<td>Frank</td>
</tr>
<tr>
<td>Ted</td>
<td>No name</td>
<td>No name</td>
</tr>
</table>
6.更换 (6. replace)
The replace filter formats a given string by replacing the placeholders. The replace filter has one parameter which is an object. The key specifies what needs to be replaced and the corresponding value specifies what it needs to be replaced with.
替换过滤器通过替换占位符来格式化给定的字符串。 替换过滤器具有一个作为对象的参数。 键指定需要替换的内容,相应的值指定需要替换的内容。
Note that using % as a delimiter purely conventional and thus optional.
请注意,使用%作为分隔符纯粹是常规的,因此是可选的。
{{ "This is %name% and my hobby is %hobby%."|replace({'%name%': "Fred", '%hobby%': hobby}) }}{# This is Fred and my hobby is tennis. #}{{ "I have a dog"|replace({'dog': 'cat'}) }}{# I have a cat #}
7.首先 (7. first)
The first filter returns the first element of a sequence, a mapping, or a string.
第一个过滤器返回序列,映射或字符串的第一个元素。
{{ ['a', 'b', 'c', 'd']|first }}{# outputs a #}
{{ { a: 1, b: 2, c: 3, d: 4 }|first }}{# outputs 1 #}
{{ 'abcd'|first }}{# outputs a #}
Just like there is the first filter there’s also the last filter.
就像第一个过滤器一样, 最后一个过滤器也一样。
{{ ['a', 'b', 'c', 'd']|last }}{# outputs d #}
{{ { a: 1, b: 2, c: 3, d: 4 }|last}}{# outputs 4 #}
{{ 'abcd'|last}}{# outputs d #}
8.无空间 (8. spaceless)
The spaceless filter removes whitespace between HTML tags. Note that it doesn’t remove whitespace within HTML tags.
无空间过滤器可删除HTML标签之间的空白。 请注意,它不会删除HTML标记内的空格。
This tag is meant to avoid extra whitespace between HTML tags to avoid browser rendering quirks under some circumstances. It isn’t meant to keep the size of the generated HTML to a minimum.
此标记旨在避免HTML标记之间出现多余的空格,以免在某些情况下浏览器呈现怪异。 这并不意味着将生成HTML的大小保持最小。
"<div>
<label>Some text</label>
</div>
"|spaceless }}{# output will be <div><label>Some text</label></div> #}
9.圆 (9. round)
The round filter rounds a number to a given precision. The first parameter is the precision, with a default value of 0. The second parameter is the rounding method which defaults to common. Other possible rounding methods are ceil and floor.
舍入过滤器将数字舍入到给定的精度。 第一个参数是精度,默认值为0。第二个参数是默认为common的舍入方法。 其他可能的舍入方法是ceil和floor 。
{{ 14.65|round }}{# outputs 15#}
{{ 14.65|round(1, 'floor') }}{# outputs 14.6 #}{{ 14.65|round(1, 'ceil') }}{# outputs 14.7 #}
10.按键 (10. keys)
The keys filter returns the keys of an array which comes in handy when you want to iterate over the keys of an array.
keys过滤器返回数组的键,当您要遍历数组的键时,派上用场。
{% for key in items|keys %}
...
{% endfor %}
翻译自: https://levelup.gitconnected.com/10-twig-filters-you-should-know-9719285d1f83
着色器 树枝生长