【问题标题】:How to pass a variable from Express to a Jade template如何将变量从 Express 传递到 Jade 模板
【发布时间】:2016-10-18 15:28:13
【问题描述】:

我已经看到了很多关于这个的答案,但没有一个涵盖整个过程。我真的没主意了。

这是我的代码:

快递代码

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


我真正想做的是让两个无法在模板中打印的变量。

提前致谢。

【问题讨论】:

    标签: node.js variables express pug


    【解决方案1】:

    Darren Schnare 写了一篇文章,解释了如何解决这个问题: 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
    

    【讨论】:

    • 好文章。非常感谢,鲍勃。
    • 嘿,谢谢你的回答,但假设我的翡翠模板有一个像 main.js 这样的外部脚本,我如何访问该文件中的#{working}
    猜你喜欢
    • 1970-01-01
    • 2016-10-20
    • 2014-02-09
    • 2011-08-29
    • 2012-09-01
    • 2013-11-04
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多