本文介绍了在 Jade 中使用变量包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working with Jade and Express and I would like to use a variable in my include statement. For example:

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

layout.jade

- var templates = page + '/templates/'

include templates

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

When I do this I get the error EBADF, Bad file descriptor 'templates.jade'

我什至尝试过

include #{templates}

无济于事.

推荐答案

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

AFAIK JADE does not support dynamic including. What I suggest is to "include" outside the template, i.e.

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 }

这篇关于在 Jade 中使用变量包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 01:59