gupalo / twigext
Twig 扩展
Requires
- php: >=8.1
- gupalo/arrayutils: ^1.9
- gupalo/dateutils: ^1.13
- gupalo/json: ^1.0
- symfony/property-access: ^5.4|^6.0|^7.0
- twig/twig: ^3.5
Requires (Dev)
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^10.0
README
Twig 扩展
使用
composer require gupalo/twigext
Symfony
添加到 config/services.yaml
Gupalo\TwigExt\:
resource: '../vendor/gupalo/twigext/src/TwigExt'
tags: ['twig.extension']
您可以自定义 FormatExtension
Gupalo\TwigExt\FormatExtension:
bind:
$translations:
span: div
format-date-zero: text-warning
$wrapSpan: false
ArrayExtension
uniq(items, keepKeys = false)
array_unique
如果 keepKeys = false
(默认) 则为结果返回 array_values
。
max_value(items, field)
items 中 field
值的最大值。
使用 PropertyAccessor
,因此可能是具有属性的对象。
max_ratio_value(items, field, field2)
items 中 field/field2
值的最大值。
使用 PropertyAccessor
,因此可能是具有属性的对象。
sum_value(items, field)
字段值的总和。
使用 PropertyAccessor
,因此可能是具有属性的对象。
array_sum(items)
https://php.ac.cn/manual/en/function.array-sum.php
EncodingExtension
base64_encode
https://php.ac.cn/manual/en/function.base64-encode.php
base64_decode
https://php.ac.cn/manual/en/function.base64-decode.php
md5
https://php.ac.cn/manual/en/function.md5.php
ExitExtension
exit
抛出 TwigExitException
。您的应用程序应该拦截并处理它。
FormatExtension
int
float
money
percentages
date_full
date_short
date_noyear
JsonExtension
json_decode
json_decode
数组。
如果存在无效的字符串,则不会引发错误,只会得到一个空数组。
ProgressExtension
progress_class
progress_percents
progress_int
progress_float
RandomExtension
random_hour, random_day
伪随机。默认 1..100
。
{% if random_day() <= 40 %}40% chances to see this text{% endif %}
{% if random_day() > 40 %}otherwise, you'll see this text{% endif %}
{% if random_day() <= 20 %}
20% chances
{% else if random_day() <= 65 %}
45% chances
{% else %}
35% chances
{% endif %}
{% if random_day() <= 20 %}
{% set content %}
{% include '_elements/test1' ~ app.request.uri ~ '.html' %}
{% endset %}
{% else if random_day() <= 65 %}
{% set content %}
{% include '_elements/test2' ~ app.request.uri ~ '.html' %}
{% endset %}
{% else %}
{% set content %}
35% chances
{% endset %}
{% endif %}
random_day
在同一天返回相同的数字,random_hour
- 在同一小时返回相同的数字。
可选参数
max
: 如果不是1..100
,而是1..max
salt
: 如果您需要在不同的网站或甚至在同一页面上得到不同的数字
random_item
从 items 中选择一个随机的项目值
{{ random_item(['aaa', 'bbb', 'ccc']) }}
random_items
从 items 中选择多个随机项目
输出 2 个随机项目:“aaa, bbb” 或 “aaa, ccc”,或 “bbb, ccc”,或 “bbb, aaa”,或 “ccc, aaa”,或 “ccc, bbb”。
{{ random_items({'k1': 'aaa', 'k2': 'bbb', 'k3': 'ccc'}, 2)|join(', ') }}
相同但保留键
{{ random_items({'k1': 'aaa', 'k2': 'bbb', 'k3': 'ccc'}, 2, true)|json_encode }}
如果 items 为空,则返回默认值(第四个参数):示例中的 “ouch”。默认 - []
。
{{ random_items({}, 2, true, 'ouch')|json_encode }}
如果您请求的项目多于 items 中的项目数,则将返回所有项目,但会打乱顺序:“aaa, bbb” 或 “bbb, aaa”
{{ random_items({'k1': 'aaa', 'k2': 'bbb', 1000)|join(', ') }}
StringExtension
truncate(s, length = 100)
限制字符串长度。如果更长,则添加 …
(...
)
underscore(s, character = '_')
将文本转换为下划线
{{ 'CamelCase? ok'|undersore }}
将返回 camel_case? ok
mask_password(s)
仅保留前 3 个字母并隐藏下一个
{{ 'mySuperPassword'|mask_password }}
将显示 myS*****
。
safe_title(s)
准备要插入到 HTML 属性中的值。
<a title="{{ unsafe_text|safe_title }}">hello</a>