pugjs able to use ternary for text?

冷暖自知 提交于 2020-01-23 20:24:21

问题


I know we are able to use ternary for attr but I am wondering if we can do it with plain text?

let's say I have something like this

        button.btn.btn-round.btn-info
            |
            if image
                | Change Image
            else
                | Add Image

the button will have the text either Change Image or Add image if image variable exists

Is this possible to use ternary? Thanks in advance for any help and suggestions


回答1:


Yes, it's absolutely possible. Just use the equals sign at the end of the element definition to get pug to evaluate what follows as an expression, then use the ternary operator to spit out a string:

button.btn.btn-round.btn-info= image ? "Change Image" : "Add Image"

You can do the same thing for an attribute:

button(class= image ? "green" : "red")


来源:https://stackoverflow.com/questions/52488422/pugjs-able-to-use-ternary-for-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!