目录

Grav - Twig过滤器和功能( Twig Filters & Functions)

在本章中,我们将研究Twig Filters and Functions 。 过滤器也用于根据需要使用所需的输出格式化数据。 函数用于生成内容。

Twig模板是包含由值替换的表达式和变量的文本文件。 Twig使用三种类型的标签。

  • Output tags - 以下语法用于在此处显示已计算的表达式结果。

{{  Place Your Output Here  }}
  • Action Tags - 以下语法用于在此处执行语句。

{%  executable statements are placed here  %}
  • Comment tags - 以下语法用于在Twig模板文件中编写注释。

{#  write your comment here  #}

树枝过滤器

Twig Filters使用| 将过滤器应用于Twig变量后跟过滤器名称的字符。 类似于Twig函数,参数可以在括号中传递。

下表显示了Grav中使用的Twig过滤器 -

Sr.No. 过滤和说明
1

Absolute URL

它采用相对路径并将其转换为绝对URL。

'<img src="/some/path/img.jpg"/>' |absolute_url

转换为 -

<img src="http://learn.getGrav.org/some/path/img.jpg" />
2

Camelize

它将字符串转换为CamelCase格式。

'contact_us'| camelize

转换为 -

ContactUs
3

Contains

如果找到字符串。

'This is some string' | contains('some')

输出是 -

1
4

Defined

您可以检查是否定义了某个变量。 如果未定义变量,则可以提供默认值。

set header_image_width = 
page.header.header_image_width|defined(900)

如果未定义,则将header_image_width为值900。

5

Ends-With

您可以使用Ends-With过滤器确定字符串是否以给定字符串结尾。

'this is an example for ends-with filter' | ends_with('filter')

它显示为 -

True
6

FieldName

它通过将点更改为数组表示法来过滤字段名称。

'field.name'|fieldName

它显示为 -

field[name]
7

Humanize

它用于将字符串转换为人类可读格式。

'some_text_to_read'|humanize

它显示为 -

Some text to read
8

Ksort

它使用键对数组映射进行排序。

{% set ritems = {'orange':1, 'apple':2, 'peach':3}|ksort %}
{% for key, value in ritems %}{{ key }}:{{ value }}, {% endfor %}

它显示为 -

apple:2, orange:1, peach:3,
9

Left Trim

它用于删除字符串开头的空格,并删除字符串左侧给出的匹配字符。

'/strip/leading/slash/'|ltrim('/')

它显示为 -

strip/leading/slash/
10

Markdown

它用于使用Grav的markdown解析器将包含markdown的字符串转换为HTML。

'## some text with markdown'|markdown

它显示为 -

<h2>some text with markdown</h2>
11

MD5

可以使用此过滤器创建字符串的md5哈希。

'something'|md5

它显示为 -

437b930db84b8079c2dd804a71936b5f
12

Monthize

通过使用Monthize过滤器,我们可以将整数天转换为月数。

'61'|monthize

它显示为 -

2
13

Nice Time

通过使用Nice Time过滤器,我们可以获得一个很好的人类可读时间格式的日期作为输出。

page.date|nicetime(false)

它显示为 -

3 hrs ago
14

Ordinalize

通过使用Ordinalize过滤器,可以将标准(如第1, 2,第3)赋予整数。

'78'| ordinalize

它显示为 -

78th
15

Pluralize

可以使用Pluralize过滤器将字符串转换为其复数英语形式。

'child'|pluralize

它显示为 -

children
16

Randomize

此过滤器有助于随机化提供的列表。 如果参数包含任何值,则从随机化中跳过这些值。

{% set ritems = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']|randomize(3) %}
{% for ritem in ritems %}{{ ritem }}, {% endfor %}

它显示为 -

one, two, three, eight, six, five, nine, seven, ten, four,
17

Right Trim

它与left trim非常相似,只是它从字符串的右侧删除了空格和匹配的字符。

'/strip/leading/slash/'|rtrim('/')

它显示为 -

/strip/leading/slash
18

Singularize

可以使用奇异滤波器将字符串转换为英文单数形式。

'vehicles'|singularize

它显示为 -

vehicle
19

Safe Email

安全电子邮件过滤器用于将电子邮件地址转换为ASCII字符,以便使电子邮件更难发送垃圾邮件。

"someoneemailaddress@domain.com"|safe_email

输出是 -

someoneemailaddress@domain.com
20

SortByKey

它用于使用键对数组映射进行排序。

{% set people = [{'email':'john@gmail.com', 'id':3}, {'email':'melw@fdd.com', 'id':1}, {'email':'nancy@fb.com', 'id':7}]|sort_by_key('id') %}
{% for person in people %}{{ person.email }}:{{ person.id }}, {% endfor %}

它显示 -

melw@fdd.com:1, john@gmail.com:3, nancy@fb.com:7,
21

Starts-With

您可以使用Starts-With过滤器确定字符串是否以给定字符串开头。

'this is an example for starts-with filter' |starts_with('this')

输出是 -

true
22

Translate

了解更多详细信息。

MY_LANGUAGE_KEY_STRING

它显示 -

'Some text in English'
23

Translate Admin

它将字符串转换为当前语言,该语言在user.yaml文件中设置。

24

Titleize

使用Titleize将字符串转换为Title Case格式。

'welcome page'|titleize

它显示为 -

Welcome Page
25

UnderScoreize

使用UnderScoreize过滤器格式化。

'ContactUs'|underscorize

它被转换为 -

contact_us
26

Truncate a string

您可以使用Truncate截断字符串或​​缩短字符串,您必须指定字符数。

'one sentence. two sentences'|truncate(5)

它截断为 -

one s...

如果您不希望在给定的字符数后将字符串截断为最接近的句末,则可以使用true作为参数。

'one sentence. two sentences'|truncate(5, true)

它截断为 -

one sentence
您也可以删除HTML文本,但在truncate过滤器之前应使用striptags过滤器。
'<p>one <strong>sentence<strong>. two sentences</p>'|striptags|truncate(5)

它显示为 -

one s

Twig 函数

通过传递参数直接调用Twig函数。 下表列出了这些功能 -

Sr.No. 功能说明
1

Array

此函数将值转换为数组。

array(value)
2

Authorize

此功能使经过身份验证的用户有权查看资源并接受权限字符串或权限字符串数组。

authorize(['admin.statistics', 'admin.super'])
3

Dump

它接受一个有效的twig变量并将其转储到Grav调试器面板中。 但是,应启用调试器以查看消息选项卡值。

dump(page.header)
4

Debug

这与dump()函数相同。

5

Gist

此函数根据Github Gist ID创建Gist嵌入代码。

6

Random String Generation

此函数将创建具有指定字符数的随机字符串。 这些字符串可以用作唯一ID或密钥。

generate_random_string(10)
7

Repeat

此函数将在给定的时间内重复该字符串。

repeat('Grav ', 10) will repeat Grav 10 times.
8

String

生成指定字符长度的随机字符串。

ta (23)
9

Translate Array

它是与|ta过滤器连接的功能。

10

Url

此过滤器将创建一个URL,它还将PHP URL流转换为有效的HTML资源。 如果无法解析URL,则可以传递默认值。

url('theme://images/logo.png') | default('http://www.placehold.it/150x100/f4f4f4')
11

Translate

使用Translate过滤器,字符串被转换为|t过滤器。

t('SITE_NAME')

被翻译成 -

Site Name
↑回到顶部↑
WIKI教程 @2018