【问题标题】:Two html classes with twig variables两个带有 twig 变量的 html 类
【发布时间】:2019-02-14 03:26:10
【问题描述】:

我想要一个 html 元素,其中包含两个由 twig 变量定义的类。 我可以用一个 twig 变量来做到这一点,它们都单独工作。但是一旦我尝试将它们放在一起,只有第一堂课是有效的。 我在论坛上搜索过,但只找到了 html 中的 twig 类或两个类。 有了 Twig 我有:

<p class={{"type#{item.type1}"}}> Paragraph </p>

在 html 中应该是:

<p class="type1 type2"}}

当我尝试将两者组合如下时,它不起作用:

<p class={{"type#{item.type1} type#{item.type2}"}} > Paragraph </p>

我也尝试了~ 的其他 concat 方法,但没有成功。 How to concatenate strings in twig

我尝试添加&amp;nbsp;而不是空格,正如这里解释的那样,也没有成功: How to add space between variables in twig template?

【问题讨论】:

    标签: html class twig


    【解决方案1】:

    您忘记了将属性值用引号括起来。 HTML 会将第二类视为另一个属性,而不是类属性的一部分

    <p class="{{"type#{item.type1} type#{item.type2}"}}">Paragraph</p>
    

    【讨论】:

    • 谢谢!有效。我认为 {{ }} 中包含的引号包含了属性。
    • 否,因为 twig 会将它们视为字符串的开头和结尾,例如 {{ "foo" }} 将输出 foo
    猜你喜欢
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多