node.js 在 Jade 中使用变量包含
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12132978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use a variable in a Jade include
提问by Spencer Carnage
I'm working with Jade and Express and I would like to use a variable in my include statement. For example:
我正在使用 Jade 和 Express,我想在我的 include 语句中使用一个变量。例如:
app.js
应用程序.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
When I do this I get the error EBADF, Bad file descriptor 'templates.jade'
当我这样做时,我收到错误 EBADF, Bad file descriptor 'templates.jade'
I even tried
我什至试过
include #{templates}
to no avail.
无济于事。
采纳答案by freakish
AFAIK JADE does not support dynamic including. What I suggest is to "include" outside the template, i.e.
AFAIK JADE 不支持动态包含。我的建议是在模板之外“包含”,即
app.js
应用程序.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
layout.jade
|!{ html }
回答by antpaw
this also works:
这也有效:
//controller
var jade = require('jade');
res.render('show', {templateRender: jade.renderFile});
//template
!= templateRender('my/path/'+dynamic+'.jade', options)
This probably will not increase the performance that you would expect from using the 'view cache'setting (it's on by default in NODE_ENV === 'production'). Or even break the app (e.g. if files are not available on the hard drive while deploying new code). Also trying to use this trick in a client-side or isomorphic app will not work because the template can not be compiled.
这可能不会提高您使用“视图缓存”设置所期望的性能(默认情况下,它在 NODE_ENV === 'production' 中启用)。甚至破坏应用程序(例如,如果部署新代码时硬盘驱动器上没有文件)。同样尝试在客户端或同构应用程序中使用此技巧将不起作用,因为无法编译模板。
回答by Frijol
Found this page googling for the same question, but in a different context, so thought I'd put my solution (read: workaround) here for posterity:
发现这个页面在谷歌上搜索同样的问题,但在不同的上下文中,所以我想我会把我的解决方案(阅读:解决方法)放在这里供后代使用:
I wanted to surround my include with more context pulled from the variable, e.g. (simplified):
我想用从变量中提取的更多上下文来包围我的包含,例如(简化):
- var templates = page + '/templates/'
- var headid = page + 'head'
- var imgsrc = '/images/' + page
div(id=headid)
h1 #{page}
img(src=imgsrc)
div(id=page)
include templates
Since that doesn't work (Jade doesn't support dynamic includes, as noted by freakish), I hybridized with a mixin:
由于这不起作用(如 freakish 所指出的,Jade 不支持动态包含),我与 mixin 混合:
(Edit– a little more elegant than my previous workaround:)
(编辑 - 比我以前的解决方法更优雅:)
mixin page1
include page1/templates
mixin page2
include page2/templates
...
- for (var i = 0; i < 3; i++)
- var page = 'page' + i
- var headid = page + 'head'
- var imgsrc = '/images/' + page
div(id=headid)
h1 #{page}
img(src=imgsrc)
div(id=page)
+page
My previous answer:
我之前的回答:
mixin templates(page)
- var headid = page + 'head'
- var imgsrc = '/images/' + page
div(id=headid)
h1 #{page}
img(src=imgsrc)
+templates('page1')
#page1
include page1/templates/
+templates('page2')
#page2
include page2/templates/
...
It's not elegant, and it won't work if you need to include more than a few things this way, but at least part of the Jade is dynamic.
它并不优雅,如果您需要以这种方式包含多个内容,它也行不通,但至少 Jade 的一部分是动态的。
回答by user2956171
Why do not use jade inheritance?
为什么不使用玉继承?
Render what you want at middleware level:
在中间件级别呈现您想要的内容:
res.render('templates/' + template_name + '.jade')
Write common common.jade:
写通用common.jade:
h1 This is a page
.container
block sublevel
h2 Default content
Then write file that extends common.jade:
然后写入扩展的文件common.jade:
extends common.jade
block sublevel
h2 Some things are here
回答by robertfoenix
It's 2019 and using variables in Pug (previously Jade) mixins has become simple.
现在是 2019 年,在 Pug(以前称为 Jade)mixin 中使用变量变得很简单。
When creating your mixin, you can give it parameters as per value(s) you're expecting to pass to the mixin. You can access any nested values using dot notation.
创建 mixin 时,您可以根据希望传递给 mixin 的值为其提供参数。您可以使用点表示法访问任何嵌套值。
mixinFile.pug:
mixinFile.pug:
mixin myMixin(parameter1, parameter2, parameter3)
h2.MyHeading #{parameter1}
p.MyParagraph #{parameter2.myVariable}
.MyBox(id= parameter3.id)
index.pug:
索引.pug:
include mixinFile
block content
+MyMixin(variable1, variable2, variable3)
You can read more in the official Pug documentation on Mixins.
你可以在关于 Mixins的官方 Pug 文档中阅读更多内容。

