简体   繁体   English

用不同的数据编译Jade部分

[英]Compiling Jade partial with different data

I am using gulp to compile Jade for a static website. 我正在使用gulp编译静态网站的Jade。 There is a single gulp task that compiles all the jade files into HTML files. 有一个gulp任务可以将所有的jade文件编译成HTML文件。

I am creating a 10 step process, each a single page of HTML with "previous" and "next" buttons 我正在创建一个10步骤的过程,每个HTML页面带有“上一个”和“下一个”按钮

I want to create a partial like below 我想创建如下的局部

a(href="#{prev}") Back
a(href="#{next}") Continue

For each page, the prev and next values change. 对于每一页,上一个和下一个值都会更改。 Is there a way to call the partial from within each page's jade with custom prev and next values? 有没有一种方法可以使用自定义的prev和next值从每个页面的翡翠中调用局部?

I am assuming, like how you bind data in handlebars template and compile, I can have a different locals object for each page and render the same partial with different data. 我假设,就像您如何在把手模板中绑定数据并进行编译一样,我可以为每个页面使用不同的locals对象,并使用不同的数据呈现相同的部分。

Am I approaching this wrong or is this something possible with jade? 我是在处理这个错误,还是玉器有可能? All answers I can see are related to using express with Jade. 我看到的所有答案都与在Jade中使用express有关。 I'm only creating a static website, just the HTML alone infact. 我只是在创建一个静态网站,而实际上只是HTML。

If you're including the partial within larger Jade templates via include then it's simply a matter of changing the locals for the larger template you're rendering. 如果您通过include将更大的Jade模板包含在局部中,那么只需更改要渲染的更大模板的局部变量即可。

gulp.src('./templates/template-that-includes-a-partial.jade')
  .pipe(gulpJade({
    locals: {
      prev: 'some value',
      next: 'some other value'
    }
  })
  .dest('./build/templates/');

Something like that should work. 这样的事情应该起作用。 The partial view should have access to the same locals as the parent view that includes it. 局部视图应该具有与包含局部视图的父视图相同的本地权限。

I found out that you can define Jade variables using - in the parent template and call the partial with this data 我发现您可以在父模板中使用-定义Jade变量,并使用此数据调用局部变量

- var prev = "a.html"
- var next = "b.html"

include partials/_var

And then use interpolation in the partial to use corresponding values 然后在部分中使用插值以使用相应的值

a(href="#{prev}") Prev
a(href="#{next}") Next

This way, I can call the same partial in different parent templates but pass in varying values for each page. 这样,我可以在不同的父模板中调用同一部分,但为每个页面传递不同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM