简体   繁体   English

从扩展的pug / jade模板将变量传递给基本布局

[英]Pass variables to base layout from extending pug/jade template

I would like to set a class on the body tag by declaring a variable in a template that extends a base layout. 我想通过在扩展基本布局的模板中声明一个变量来在body标签上设置一个类。

When I try, the body_class variable is undefined in the layout. 当我尝试时, body_class变量在布局中是undefined的。

It appears the layout is executed before the extending template, or they are executed in different scopes or something. 看起来布局在扩展模板之前执行,或者它们在不同的范围或其他内容中执行。

Is there another way? 还有另外一种方法吗? Would a mixin work here? mixin能在这里工作吗?

_layout.jade: _layout.jade:

doctype html
html(lang="en-au")
    head
        meta(charset="utf-8")
        block css
    body(class=(body_class || "it-did-not-work"))
        block header
        block content
        block footer

home.jade: home.jade:

var body_class = 'i-am-the-home-page'
extends _layout
block header
    h1 home

Ah ha! 啊哈! Figured it out. 弄清楚了。

Create a block at the top of the base layout and add your variables in there. 在基本布局的顶部创建一个块,并在其中添加变量。

_layout.jade: _layout.jade:

block variables
doctype html
html(lang="en-au")
    head
        meta(charset="utf-8")
        block css
    body(class=(body_class || "it-did-not-work"))
        block header
        block content
        block footer

home.jade: home.jade:

extends _layout
block variables
    - var body_class = 'i-am-the-home-page'
block header
    h1 home

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

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