2016-11-02 7 views
0

文字列の一部をTwigのアスタリスクに置き換えます。例えばTwig文字列の一部を選択した文字に置き換えます。

SomePartlyVisibleStringHere 

私はそのような結果持って、アスタリスクでこの文字列内の4番目の後のすべての文字を変更したい:

Some********************* 

は新しい小枝を定義することなく行うことが可能ですヘルパー?

+0

あなたの問題を解決した場合は、正しい答えとして記入してください。 – Rhono

答えて

2

macro(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******************* #} 

例:https://twigfiddle.com/aobt8s

1

これが私の仕事:あなたはそれを私がmaking a custom filterを示唆している複数回行う必要がある場合は

{% set string = 'SomePartlyVisibleStringHere' %} 
{% set starCount = string|length - 4 %} 
{{ string[:4] }}{% for i in 1..starCount %}*{% endfor %} 

は、その後、あなただけ行うことができます。

{% set string = 'SomePartlyVisibleStringHere' %} 
{{ string|customFilterName }} 
関連する問題