Pug 语法中的三元运算符 [英] Ternary operator in Pug syntax

查看:11
本文介绍了Pug 语法中的三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pug 代码中可以使用三元运算符吗?

Is it possible to use ternary operator in Pug code ?

我的尝试:

哈巴狗文件

- var disabled = true
input(type="checkbox" disabled ? disabled : null)

HTML 结果

<input disabled ? disabled : null></input>

HTML 想要的结果

<input disabled></input>

我知道描述 here 的标准条件 Pug 语法,但很难相信在现代 Pug 语法中没有机会使用三元运算符.

I know about standard conditional Pug syntax which described here, but it hard to believe that there's no chance of using ternary operator in modern Pug syntax.

谢谢你帮助我!

推荐答案

你已经很接近了 - Pug 只需要一个属性来开始处理.您的代码只是在进行评估,不属于任何属性.

You're very close - Pug just needs an attribute to start the processing. Your code was just doing an evaluation without belonging to any attribute.

因此,只需在前面添加您要评估的属性,然后将三元运算符放在其后面.在这种情况下,您只希望 disabled 出现,仅此而已,所以让我们使用空字符串表示 true,使用 null 表示 false,让 pug 知道该怎么做.

So just add the attribute that you want evaluated up front then put the ternary operator after it. In this case you just want disabled to appear and nothing more, so let's use an empty string for true and null for false to let pug know what to do.

它在 codepen 中工作.

div
  - var disabled = true;
  input(disabled = disabled ? '' : null)

使用 true,pug 生成 <input disabled>.

With true, pug generates <input disabled>.

使用 false,pug 生成 <input>.

With false, pug generates <input>.

这篇关于Pug 语法中的三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆