简体   繁体   English

如何用当前年份替换存储在 Drupal Twig 模板中的对象/数组属性中的占位符字符串?

[英]How to replace a placeholder string stored in an object/array property in a Drupal Twig template with the current year?

I have a custom block which returns an array with a text-field.我有一个自定义块,它返回一个带有文本字段的数组。

How can I replace the string %current_year% in the #text property of the array with the current year in my twig template?如何将数组的#text属性中的字符串%current_year%替换为树枝模板中的当前年份?

'0' => array(4)
   '#type' => string(14) "processed_text"
   '#text' => string UTF-8(119) "The current year is %current_year%."

I'm assuming your array/object is passed to the template as the variable myObject .我假设您的数组/对象作为变量myObject传递给模板。

You can render the string in the #text property while replacing the placeholder as follows:您可以在替换占位符的同时呈现#text属性中的字符串,如下所示:

{{ attribute(myObject, '#text') | replace({"%current_year%": ("now"|date("Y"))}) }}

... or use an intermediate variable ... ...或使用中间变量 ...

{% set currentYear = "now"|date("Y") %}
{{ attribute(myObject, '#text') | replace({"%current_year%": currentYear}) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM