通过 Jade 模板传递变量 [英] Passing variables through Jade templates
问题描述
在 Jade 中可以将变量从一个模板传递到另一个模板吗?.我想做这样的事情:
It's possible in Jade to pass variables from one template to another?. I want to do something like this:
tmp1.jade
div.anyClass
include components/checkbox('someLabel')
tmp2.jade
div.otherClass
div.label
{someLabel}
谢谢!
推荐答案
包含的模板继承包含它们的模板的变量范围,因此您所追求的将自动发生.
Included templates inherit the variables scope of the template that included them, so what you're after will happen automatically for you.
所以以下将起作用:
tmp1.jade
- var label = 'value'
div.anyClass
include tmp2
tmp2.jade
div.otherClass
div.label
#{label}
你也可以使用mixin来传递变量,它们就像函数一样(你先定义它们,然后调用它们)
You can also use mixins to pass variables, they are like functions (you define them first, then call them)
因此您可以执行以下操作:
So you could do the following:
tmp1.jade
mixin labeldiv(myLabel)
div.otherClass
div.label
#{myLabel}
div.anyClass
+labelDiv("the label")
值得一提的是,如果您希望它们在多个模板中通用,您还可以将 mixin 放入包含中.你可以这样做:
It's worth mentioning that you can also put mixins inside includes, if you want them to be common across multiple templates. You could do this:
myMixins.jade
mixin labeldiv(myLabel)
div.otherClass
div.label
#{myLabel}
tmp1.jade
include myMixins
div.anyClass
+labelDiv("the label")
Jade Syntax Docs 有一些很棒的(实时)示例说明一切有效.
The Jade Syntax Docs have some great (live) examples of how it all works.
这篇关于通过 Jade 模板传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!