【问题标题】:Node.js express: confuse about ejs templateNode.js express:对 ejs 模板感到困惑
【发布时间】:2012-08-13 14:36:50
【问题描述】:

我将ejs 模板文件放在views 文件夹中,例如:

views
|
|--foo.html
|
|--layout.html

所以我配置了我的 ejs 模板:

app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);  

我通过这个渲染我的foo 模板:

res.render('foo.html', {title: 'test'});

我想使用layout.html作为主布局模板,我也在layout.html中添加了<%- body%>标签,但是不起作用,我看到的只是返回结束的foo.html

为什么layout.html 不能作为主布局?或者如何将其设置为主布局?

【问题讨论】:

  • 如果您愿意/可以在官方文档更新时继续使用 Express 2.x 并使用 EJS。

标签: node.js express ejs


【解决方案1】:

啊,您刚刚被 Express 3 在布局管理中的更改所欺骗。 官方示例:https://github.com/visionmedia/express/tree/master/examples/ejs 尚未更新。

在 Jade 中,您现在必须使用块并扩展布局。 http://www.devthought.com/code/use-jade-blocks-not-layouts/

【讨论】:

  • 感谢 URL “examples/ejs”。它确实帮助我理解了使用 ejs、express 和 Node.js 的模板
【解决方案2】:

尝试使用 ejs-locals 然后您可以在 login.ejs 文件中执行此操作:

<% layout('layout') -%>
<form></form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 2014-11-27
    • 2022-01-04
    相关资源
    最近更新 更多