简体   繁体   English

Twig:连接字符串中的变量

[英]Twig: concatenate variable in string

I've just begun learning Twig and i'm really stuck at this stupid little issue. 我刚刚开始学习Twig,我真的陷入了这个愚蠢的小问题。 Concatenating this doesn't seem to work (eigenKleurInput will ultimately be a value): 串联起来似乎不起作用(eigenKleurInput最终将是一个值):

{% set eigenKleurInput = "acefbf" %}
{% set customBackgroundColorInline = 'style=background-color: #' ~ eigenKleurInput %}

The output variable "customBackgroundColorInline" is put inside a div: 输出变量“ customBackgroundColorInline”放在div中:

<section {{ customBackgroundColorInline }}>

Desired output would be 所需的输出将是

<section style="background-color: #xxx">

Thank you very much! 非常感谢你!

If I correctly understand your question the problem is about encoded character: if you add the " in your code twig render as &quot; . 如果我正确理解了您的问题,那么问题就出在编码字符上:如果在代码树枝渲染中添加" &quot;

In this case you should use the raw filter as follow: 在这种情况下,应按以下方式使用原始过滤器

{% set eigenKleurInput = "acefbf" %}
{% set customBackgroundColorInline = 'style="background-color: #' ~ eigenKleurInput ~ '"' %}


 <section {{ customBackgroundColorInline|raw }}>

So the output will be: 因此输出将是:

<section style="background-color: #acefbf">

You could try online in this working twigfiddle 您可以在此工作中尝试在线

Hope this help 希望这个帮助

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

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