如何用当前年份替换 Drupal Twig 模板中 object/array 属性 中存储的占位符字符串?
How to replace a placeholder string stored in an object/array property in a Drupal Twig template with the current year?
我有一个自定义块,其中 returns 一个带有文本字段的数组。
如何用我的 twig 模板中的当前年份替换数组 #text
属性 中的字符串 %current_year%
?
'0' => array(4)
'#type' => string(14) "processed_text"
'#text' => string UTF-8(119) "The current year is %current_year%."
我假设您的 array/object 作为变量 myObject
传递给模板。
您可以在 #text
属性 中呈现字符串,同时替换占位符,如下所示:
{{ attribute(myObject, '#text') | replace({"%current_year%": ("now"|date("Y"))}) }}
...或使用中间变量...
{% set currentYear = "now"|date("Y") %}
{{ attribute(myObject, '#text') | replace({"%current_year%": currentYear}) }}
我有一个自定义块,其中 returns 一个带有文本字段的数组。
如何用我的 twig 模板中的当前年份替换数组 #text
属性 中的字符串 %current_year%
?
'0' => array(4)
'#type' => string(14) "processed_text"
'#text' => string UTF-8(119) "The current year is %current_year%."
我假设您的 array/object 作为变量 myObject
传递给模板。
您可以在 #text
属性 中呈现字符串,同时替换占位符,如下所示:
{{ attribute(myObject, '#text') | replace({"%current_year%": ("now"|date("Y"))}) }}
...或使用中间变量...
{% set currentYear = "now"|date("Y") %}
{{ attribute(myObject, '#text') | replace({"%current_year%": currentYear}) }}