本文介绍了玉模板 - 动态调用Mixin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用json中的字符串输入Jade模板来动态加载mixin?下面,目标是 twoColumn.jade
加载 foo
和栏
mixins。
How can I use a string from the json being fed into a Jade template to dynamically load a mixin? Below, the goal is for twoColumn.jade
to load the foo
and bar
mixins.
twoColumn.jade
mixin twoColumns(obj)
.container-fluid
.row(class=obj.class)
for item in obj.items
.col-xs-12.col-sm-3
//- Syntax for dynamically calling a mixin?
+item.template(item)
content.json
{
"twoColumns": {
"class": "foobar",
"items": [
{
"template": "foo",
"title": "Hello"
},
{
"template": "bar",
"title": "World"
}
]
}
}
推荐答案
这是一个在Jade中不是很明显的功能,因为它没有在文档中明确提到。您实际上可以使用插值语法(#{...}
)来动态选择 mixin
名称。
This is a feature that is not very obvious in Jade, as it is not explicitly mentioned in the documentation. You can actually use the interpolation syntax (#{...}
) for dynamically choosing the mixin
name.
来自 :
#user #{name} <#{email}>
输出< div id =user> tj& lt ; tj@vision-media.ca& gt;< / div>
示例用法:
mixin foo(item)
p Foo called
mixin bar(item)
p Bar called
mixin twoColumns(obj)
.container-fluid
.row(class=obj.class)
for item in obj.items
.col-xs-12.col-sm-3
+#{item.template}(item)
这篇关于玉模板 - 动态调用Mixin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!