【问题标题】:pug-php inline attribute conditionpug-php 内联属性条件
【发布时间】:2017-12-03 21:02:14
【问题描述】:

我最近开始使用 Pug,并在 php 环境中试用。

我遇到了将内联条件应用于属性的问题。

我正在尝试做的事情如下:

$variable = 'red';  

div.class(
    if($variable):
        style="background-color:".$variable
    endif:
)

所以我需要做的是让样式属性甚至不显示变量是否为空或不存在。

提前致谢!

【问题讨论】:

    标签: php html conditional pug templating


    【解决方案1】:

    作为一种替代方法,我建议在实际属性值中设置条件:

    div.class(style=($variable ? "background-color:".$variable : undefined))

    我不确定这在 Pug PHP 中是否像在普通 Pug 中一样有效,因为我没有可用的该组合的测试设置。然而,我所知道的是,Pug 删除了任何具有 undefined 作为值的属性,这意味着 style 属性应该只在 $variable 评估为真实值时出现,这应该对应于你正在尝试的东西在这里实现。

    【讨论】:

      猜你喜欢
      • 2019-01-30
      • 2017-06-07
      • 2018-02-16
      • 2022-11-11
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 2019-10-01
      • 2012-01-23
      相关资源
      最近更新 更多