将 Pug mixin 分配给变量
Assign Pug mixin to variable
有什么方法可以将 Pug mixin 分配给变量吗? (N.b。不是 mixin 的结果。)
即像这样:
mixin a
...
mixin b
...
mixin c
...
-
let myMixin;
switch (someCondition) {
case 1: myMixin = b; break;
case 2: myMixin = c; break;
default: myMixin = a;
}
...And then use it like this further down in the template:
+myMixin
您可以在 Pug 中使用动态混合:
+#{mixinName}()
创建一个生成动态混合的混合,例如:
mixin dynMixin(name)
+#{name}()
然后使用动态混合很容易:
mixin a
p AAA
mixin b
p BBB
mixin c
p CCC
-
let myMixin;
let someCondition = 2;
switch (someCondition) {
case 1: myMixin = 'b'; break;
case 2: myMixin = 'c'; break;
default: myMixin = 'a';
}
+dynMixin(myMixin)
//- or write directly: +#{myMixin}()
注意:动态mixin的名称必须是字符串
有什么方法可以将 Pug mixin 分配给变量吗? (N.b。不是 mixin 的结果。)
即像这样:
mixin a
...
mixin b
...
mixin c
...
-
let myMixin;
switch (someCondition) {
case 1: myMixin = b; break;
case 2: myMixin = c; break;
default: myMixin = a;
}
...And then use it like this further down in the template:
+myMixin
您可以在 Pug 中使用动态混合:
+#{mixinName}()
创建一个生成动态混合的混合,例如:
mixin dynMixin(name)
+#{name}()
然后使用动态混合很容易:
mixin a
p AAA
mixin b
p BBB
mixin c
p CCC
-
let myMixin;
let someCondition = 2;
switch (someCondition) {
case 1: myMixin = 'b'; break;
case 2: myMixin = 'c'; break;
default: myMixin = 'a';
}
+dynMixin(myMixin)
//- or write directly: +#{myMixin}()
注意:动态mixin的名称必须是字符串