node.js - 在 Jade 包含中使用变量

标签 node.js express pug

我正在使用 Jade 和 Express,我想在我的 include 语句中使用一个变量。例如:

app.js

app.get('/admin', function (req, res) {
  var Admin = require('./routes/admin/app').Admin;

  res.render(Admin.view, {
    title: 'Admin',
    page: 'admin'
  });
});

layout.jade

- var templates = page + '/templates/'

include templates

当我这样做时,我收到错误 EBADF, Bad file descriptor 'templates.jade'

我什至尝试过

include #{templates}

无济于事。

最佳答案

AFAIK JADE 不支持动态包含。我的建议是在模板之外“包含”,即

app.js

app.get('/admin', function (req, res) {
    var Admin = require('./routes/admin/app').Admin;
    var page = 'admin';

    var templates = page + '/templates/';

    // render template and store the result in html variable
    res.render(templates, function(err, html) {

        res.render(Admin.view, {
            title: 'Admin',
            page: page,
            html: html
        });

    });

});

layout.jade

|!{ html }

关于node.js - 在 Jade 包含中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12132978/

相关文章:

node.js - Sequelize 包含关联不起作用(graphql)

express - (Next.js,Express session )在 getInitialProps 内发出的每个请求的新 session

javascript - Jade /Expressjs : Pass objects from server to client

node.js - 列出文件时,node.js 出现类型错误

node.js - 如何使用 Node.js/Jade 渲染嵌套数据结构

node.js - 安装时出现奇怪的良性 Node.JS 错误/警告

javascript - 如何在javascript中动态增加变量名称?

mysql - MySQLx 尝试解析表达式时出错(Nodejs)

node.js - Passport Js 处理 HTTP API 的访客身份验证

javascript - 我来自 Node 的数据没有绑定(bind)到模板变量