Twig 细枝用所选字符替换字符串的一部分

Twig 细枝用所选字符替换字符串的一部分,twig,Twig,我想用小树枝上的星号替换部分字符串 例如: SomePartlyVisibleStringHere 我想用星号改变这个字符串中第四个之后的每个字母,得到这样的结果: Some********************* 可以不定义新的Twig助手吗?您可以创建一个(Twig中的一个函数)并随时调用它 {% macro redact(topSecret) %} {% set length = topSecret|length - 4 %} {{ topSecret|slice(0

我想用小树枝上的星号替换部分字符串

例如:

SomePartlyVisibleStringHere
我想用星号改变这个字符串中第四个之后的每个字母,得到这样的结果:

Some*********************
可以不定义新的Twig助手吗?

您可以创建一个(Twig中的一个函数)并随时调用它

{% macro redact(topSecret) %}
    {% set length = topSecret|length - 4 %}
    {{ topSecret|slice(0, 3) }}{% for i in 0..length %}*{% endfor %}
{% endmacro %}

{# You have to import from _self if the macro is declared in the same file. #}
{% import _self as sharpie %}

{{ sharpie.redact('Top secret information') }}
{# => Top******************* #}
示例:

您可以创建一个(Twig中的函数)并在需要时调用它

{% macro redact(topSecret) %}
    {% set length = topSecret|length - 4 %}
    {{ topSecret|slice(0, 3) }}{% for i in 0..length %}*{% endfor %}
{% endmacro %}

{# You have to import from _self if the macro is declared in the same file. #}
{% import _self as sharpie %}

{{ sharpie.redact('Top secret information') }}
{# => Top******************* #}
示例:

这对我很有效:

{% set string = 'SomePartlyVisibleStringHere' %}
{% set starCount = string|length - 4 %}
{{ string[:4] }}{% for i in 1..starCount %}*{% endfor %}
如果我建议您不止一次地这样做,那么您可以:

{% set string = 'SomePartlyVisibleStringHere' %}
{{ string|customFilterName }}
这对我很有用:

{% set string = 'SomePartlyVisibleStringHere' %}
{% set starCount = string|length - 4 %}
{{ string[:4] }}{% for i in 1..starCount %}*{% endfor %}
如果我建议您不止一次地这样做,那么您可以:

{% set string = 'SomePartlyVisibleStringHere' %}
{{ string|customFilterName }}

如果有人解决了你的问题,请将答案标记为正确。如果有人解决了你的问题,请将答案标记为正确。