Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

pug - Passing variables through Jade templates

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}

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Included templates inherit the variables scope of the template that included them, so what you're after will happen automatically for you.

So the following will work:

tmp1.jade

- var label = 'value'
div.anyClass
    include tmp2

tmp2.jade

div.otherClass
    div.label
        #{label}

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")

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")

The Jade Syntax Docs have some great (live) examples of how it all works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...