【问题标题】:PUGJS - How do I access data from locals?PUGJS - 我如何访问本地人的数据?
【发布时间】:2018-06-22 23:28:18
【问题描述】:

我正在尝试使用 PugJS 创建一个简单的模板,但我无法从传递给渲染器的对象中读取数据。这就是我所拥有的:

// main.js -- nodejs + expressjs

const defaultUser = { isAnonymous: true, name: 'test' };
app.get('/', (req, res) => {
    res.send(pug.compileFile(__dirname + '\\views\\index.pug', defaultUser));
});

// index.pug -- the template I'm having trouble with

doctype html
html
    head
        if #{isAnonymous}
            title Test page
        else
            title #{name} - Test page

我在模板的第 4 行收到语法错误(意外字符“#”):

  > 4|      if #{isAnonymous}
------------^

这是为什么呢?

【问题讨论】:

    标签: node.js express templates pug


    【解决方案1】:

    您不需要在此处引用 isAnonymous,该行以“if”开头,因此它被视为 javascript。

    doctype html
    html
        head
            if isAnonymous
                title Test page
            else
                title #{name} - Test page
    

    【讨论】:

    • 谢谢!有没有其他网站我可以查看文档?我觉得官方的太简单了。
    • 恐怕我不确定,我自己对 Pug/Jade 比较陌生,所以我主要是在阅读所有 Pug 文档和用头撞桌子之间交替进行。
    • 我明白了,谢谢!我希望对你、你的头脑和你的办公桌来说,更美好的日子即将到来:)
    猜你喜欢
    • 2018-04-14
    • 2015-06-01
    • 2019-07-12
    • 2020-07-21
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    相关资源
    最近更新 更多