从扩展 pug/jade 模板将变量传递给基本布局 [英] Pass variables to base layout from extending pug/jade template

查看:52
本文介绍了从扩展 pug/jade 模板将变量传递给基本布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在扩展基本布局的模板中声明一个变量来在 body 标记上设置一个类.

I would like to set a class on the body tag by declaring a variable in a template that extends a base layout.

当我尝试时,body_class 变量在布局中是 undefined.

When I try, the body_class variable is undefined in the layout.

看起来布局是在扩展模板之前执行的,或者它们是在不同的范围内执行的.

It appears the layout is executed before the extending template, or they are executed in different scopes or something.

还有别的办法吗?在这里可以使用 mixin 吗?

Is there another way? Would a mixin work here?

_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:

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

推荐答案

啊哈!想通了.

在基本布局的顶部创建一个块并在其中添加您的变量.

Create a block at the top of the base layout and add your variables in there.

_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:

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

这篇关于从扩展 pug/jade 模板将变量传递给基本布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆