如何在哈巴狗中使用三元运算符按条件添加标签?

How to add tag on condition with ternary operator in pug?

这工作正常

li.li
   if rout === '/about'
      | about
   else
      a(href='/about') about

有没有办法写成一行?像这样

li.li=(rout === '/about') ? 'about' : (a(href='/about') about)

即使可行,这...

li.li=(rout === '/about') ? 'about' : (a(href='/about') about)

...很乱。更好的方法是使用 mixin。

薛定谔链接混入

link === url 时,mixin returns 详细名称。在所有其他情况下,它会将 verbose name 包装在 link.


mixin schrodingerLink(link, verboseName)

   if url === link
      | #{verboseName}
   else
      a.a(href=link)= verboseName

用法:


li.li
   +schrodingerLink('/about', 'О проекте')

(与非mixin用法比较:


li.li
   if url === '/about'
      | О проекте
   else
      a.a(href='/about') О проекте

)