gpt4 book ai didi

Twig print values and strings in set statements(Twig打印set语句中的值和字符串)

转载 作者:bug小助手 更新时间:2023-10-28 10:00:58 28 4
gpt4 key购买 nike



This twig template is not DRY.

这个小树枝模板没有干。


{% set reviewer_0 = webform_token('[webform_submission:values:reviewer:0:entity:mail:clear]', webform_submission, [], options) %}
{% set reviewer_1 = webform_token('[webform_submission:values:reviewer:1:entity:mail:clear]', webform_submission, [], options) %}
{% set reviewer_2 = webform_token('[webform_submission:values:reviewer:2:entity:mail:clear]', webform_submission, [], options) %}
{% if reviewer_0|length %}{{ reviewer_0 }}{% else %}{% endif %}
{% if reviewer_1|length %},{{ reviewer_1 }}{% else %}{% endif %}
{% if reviewer_2|length %},{{ reviewer_2 }}{% else %}{% endif %}

I tried using the example from twig documentation

我尝试使用twig文档中的示例


{% for i in 0..10 %}
* {{ i }}
{% endfor %}

like this:

如下所示:


{% for i in 0..10 %}
{% set reviewer_{{ i }} = webform_token('[webform_submission:values:reviewer:'{{ i }}':entity:mail:clear]', webform_submission, [], options) %}
{% if reviewer_{{ i }}|length %}{{ reviewer_{{ i }} }}{% else %}{% endif %}
{% endfor %}

But it doesn't work. I think the strings have to be concatenated somehow. How do I create this loop in twig?

但这并不管用。我认为这些字符串必须以某种方式连接在一起。如何在树枝中创建此循环?


更多回答

Does this answer your question? How to access dynamic variable names in twig?

这回答了你的问题吗?如何访问twig中的动态变量名?

优秀答案推荐

Within {% %} you can't use {{ }} too.

在{% %}内,您也不能使用{{ }}。


First, you need to update

首先,您需要更新


webform_token('[webform_submission:values:reviewer:'{{ i }}':entity:mail:clear]', webform_submission, [], options)

into

webform_token('[webform_submission:values:reviewer:' ~ i ~ ':entity:mail:clear]', webform_submission, [], options)

and variable name seems not correct too, you can just remove _{{ i }} part. Each turn it will overridden anyway.

而且变量名似乎也不正确,可以只删除_{{i}}部分。无论如何,每一次转弯它都会被推翻。


Regards,

向您致敬,



I improved the twig based on @Emircan ERKUL's suggestion with the following:

我根据@Emircan Erkul的建议改进了小树枝,如下所示:


{% for i in 0..10 %}
{% if webform_token('[webform_submission:values:reviewers:' ~ i ~ ':entity:mail:clear]', webform_submission, [], options)|length %}
{% set reviewers = webform_token('[webform_submission:values:reviewers:' ~ i ~ ':entity:mail:clear]', webform_submission, [], options) %}
{{ reviewers }},
{% else %}{% endif %}
{% endfor %}

The comma at the end of {{ reviewers }}, is not good and results in storing reviewers like this:

{{审阅者}}末尾的逗号不正确,导致存储审阅者如下所示:


[email protected],[email protected],[email protected],

The trailing comma is trimmed off somewhere else and [email protected], is sent as [email protected]. I wouldn't mind improving the twig using a filter like the join filter except reviewers is not an array.

后面的逗号在其他地方被删除,[电子邮件受保护],作为[电子邮件受保护]发送。我不介意使用像Join筛选器这样的筛选器来改进细枝,只是审阅者不是数组。


In any case, the element now offers up to 11 reviewers using DRY code and an simple mechanism for adding more.

无论如何,Element现在提供了多达11个使用干代码的审查者和一个简单的添加更多代码的机制。


If you want to use this pattern in a Drupal webform computed twig element, here's the entire element.

如果您想在Drupal WebForm Computed twig元素中使用该模式,下面是整个元素。


reviewer_mail:
'#type': webform_computed_twig
'#title': 'Reviewer mail'
'#template': |-
{% for i in 0..10 %}
{% if webform_token('[webform_submission:values:reviewers:' ~ i ~ ':entity:mail:clear]', webform_submission, [], options)|length %}
{% set reviewers = webform_token('[webform_submission:values:reviewers:' ~ i ~ ':entity:mail:clear]', webform_submission, [], options) %}
{{ reviewers }},
{% else %}{% endif %}
{% endfor %}
'#whitespace': spaceless
'#store': true
'#ajax': true

reviewers in webform_token('[webform_submission:values:reviewers:' is another element, an Entity Select element, multiple true, target_type user, filter type role.

Webform_token(‘[webform_submission:values:reviewers:’中的审阅者是另一个元素、实体选择元素、多个TRUE、TARGET_TYPE USER、FILTER类型角色。


更多回答

Thanks @Emircan ERKUL. Unfortunately, {% for i in 0..10 %} {{ webform_token('[webform_submission:values:reviewer:' ~ i ~ ':entity:mail:clear]', webform_submission, [], options) }} {% endfor %} generates Twig syntax error: Unknown "webform_token" function in "index.html.twig" at line 2.

谢谢@Emircan Erkul。遗憾的是,{%for i in 0..10%}{{webform_token(‘[webform_submission:values:reviewer:’~i~‘:Entity:Mail:Clear]’,WebForm_Submit,[],Options)}{%endfor%}在第2行的“index.html.twig”中生成Twig语法错误:未知的“WebForm_Token”函数。

The webform knows what a webform_token is, but {% for i in 0..10 %} {% set reviewer_~ i ~ = webform_token('[webform_submission:values:reviewer:' ~ i ~ ':entity:mail:clear]', webform_submission, [], options) %} {% endfor %} throws Unexpected token "operator" of value "~" ("end of statement block" expected) in "__string_template__4c27f4a5afa0845b24e961199f41e5bd" at line 2.

WebForm知道什么是WebForm_Token,但{%for i in 0..10%}{%set Reviewer_~i~=webform_token(‘[webform_submission:values:reviewer:’~i~‘:Entity:Mail:Clear]’,WebForm_Submit,[],Options)%}{%Endfor%}在“__string_template__4c27f4a5afa0845b24e961199f41e5bd”的第2行抛出值为“~”的意外令牌“运算符”(预期为“语句结束块”)。

"~" in the left side is not correct, do not use that. Are you sure that webform_token is available? First check with webform_token('[webform_submission:values:reviewer:0:entity:mail:clear]', webform_submission, [], options) Also look; drupal.stackexchange.com/a/300055

左边的“~”是不正确的,不要用那个。您确定WebForm_Token可用吗?首先查看webform_token(‘[webform_submission:values:reviewer:0:entity:mail:clear]’,WebForm_Submit,[],Options)也可以查看;drapal.stackexchange.com/a/300055

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com