如何从 Express 传递变量到 Jade 模板

4

我已经看过很多关于这个问题的答案,但没有一个覆盖了整个过程。我真的没有头绪。

这是我的代码:

express 代码

res.render('index', {expressVar:"My Great Example"});


template.jade

doctype html

block vars

html
    head

    body
        | #{working}

        | #{notWorking}

        | #{notWorking2}


index.jade

extends template.jade

- var localVar = "Other Example"

block vars
    - var working     = "This is working"
    - var notWorking  = expressVar
    - var notWorking2 = localVar

我真正想做的是在模板中打印两个变量,但它们都无法工作。
提前感谢。
1个回答

3
达伦·斯纳尔写了一篇文章,解释了如何弄清楚这个问题:http://darrenschnare.com/blog/2013/08/14/sharing-variables-with-jade-templates/ 您需要将所有标记放置在根块中,然后在模板中声明此块中的变量。所有变量将可访问每个作用域,该作用域是该作用域的子级。 template.jade
doctype html

block page
    html
        head

        body
            | #{working}

            | #{notWorking}

            | #{notWorking2}

index.jade

extends template

- localVar = "Other Example"

prepend page
    - working     = "This is working"
    - notWorking  = expressVar
    - notWorking2 = localVar

1
很棒的文章。非常感谢,Bob。 - Marteiro
嘿,感谢您的回答,但是假设我的jade模板有一个像main.js这样的外部脚本,我该如何在那个文件中访问#{working}? - PirateApp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接