我无法在jade的类中设置变量名:
.flag_#{ session.locale } #{ session.locale }
我有:
<div class="flag_" >en</div>
我想要
<div class="flag_en" >en</div>
由于
答案 0 :(得分:75)
试试这个(尚未测试):
div(class="flag_#{ session.locale }") session.locale
答案 1 :(得分:15)
至于pug@2.0.0-alpha3是这样的:
div(class="flag_" + session.locale) session.locale
答案 2 :(得分:3)
这是另一种方法:
mixin formButton(text, type, extra_classes)
- var default_classes = 'btn btn-primary'
if extra_classes
- var classes = default_classes + ' ' + extra_classes
else
- var classes = default_classes
if type
button(class=classes type=type) !{text}
else
button(class=classes type="submit") !{text}
示例1:
用法:
+formButton('Text')
结果:
<button type="text" class="btn btn-primary" type="submit">Text</button>
示例2:
用法:
+formButton('Text', 'button')
结果:
<button type="text" class="btn btn-primary" type="button">Text</button>
示例3:
用法:
+formButton('Text', 'button', 'extra-class')
结果:
<button type="text" class="btn btn-primary extra-class" type="button">Text</button>
答案 3 :(得分:2)
您也可以使用conditional statements来执行此操作,如
if liked == true
span.like.active
else
span.like
答案 4 :(得分:0)
您也可以使用 template literal:
- const session = { locale: 'en' }
div(class=`flag_${session.locale}`) #{session.locale}
//- Just to show more options:
div(class=`flag_${session.locale}`)= session.locale
div(class=`flag_${session.locale}`) Current language is #{session.locale}
div(class=`flag_${session.locale}`)= `Current language is ${session.locale}`
输出(使用 Pug 2.0.4):
<div class="flag_en">en</div>
<div class="flag_en">en</div>
<div class="flag_en">Current language is en</div>
<div class="flag_en">Current language is en</div>