在Express 3.x中使用布局
我发现的所有指南似乎都指的是早期版本的Express,我的理解是这个function已经从2.x变成了3.x。 我假设,因为npm install express
目前安装版本3.0.3,3.x被认为是稳定的,准备生产使用。 (是不是这样?)
我想使用EJS或Kiwi模板,如果可能的话,我想在模板中提供Underscore。
但是我的第一个障碍是获得一个布局来渲染我的内容/部分内容。
鉴于:
$ express --ejs test
index.js:
exports.index = function(req, res){ res.render('index', { title: 'test' }); };
我创build了layout.ejs,但是我可以看到当我查看源代码时,它没有被调用。
将布局完全传递给模板库以处理Express 3.x(因此,如果select的模板库没有实现它们,则是SOL)?
如果它仍然是Express的一部分,我该如何configuration它? 如果必须通过模板库实现,那么EJS或Kiwi文档中的布局指示都不会显示 – 这是否意味着它们完全不被支持?
使用ejs-locals获得布局支持(也提供块和部分)…因为布局已经被删除了3.x.
https://github.com/RandomEtc/ejs-locals
从示例运行节点app.js并打开localhost:3000来查看一个工作示例。
给定一个模板,index.ejs:
<% layout('boilerplate') -%> <% script('foo.js') -%> <% stylesheet('foo.css') -%> <h1>I am the <%=what%> template</h1> <% block('header', "<p>I'm in the header.</p>") -%> <% block('footer', "<p>I'm in the footer.</p>") -%>
和布局boilerplate.ejs:
<!DOCTYPE html> <html> <head> <title>It's <%=who%></title> <%-scripts%> <%-stylesheets%> </head> <body> <header> <%-blocks.header%> </header> <section> <%-body -%> </section> <footer> <%-blocks.footer%> </footer> </body> </html>
- node.js / jQuery跨域:ERR_CONNECTION_REFUSED
- 如何使用archiver nodejs expressjs在ajax请求中下载Zip文件
- 使用Multer上传文件,并在parsing并上传到数据库之前将其暂时存储在内存中
- Sails js:如何在Sails js的单个模型中定义两个到两个不同数据库的MySQL连接?
- 在JavaScript中的ValueOf()
- Angular with CoffeeScript和Jade for前端,Node,Express和CoffeeScript用于后端
- 如何使用node.js和express来从Twitter API迭代client.get
- Express.js:如何使用基于NODE_ENV的不同中间件?
- “无法通过键查找会话:connect.sid” – session.socket.io