node.js jade 模板引擎,如何使用layout.jade?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8402568/
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
jade template engine, how to use layout.jade?
提问by Masiar
I have a website in node.js; to create a page, say mypageI noticed I need to create both a layout.jadeand mypage.jadefiles. If I put code in mypage.jadeit is not displayed, so first I have to fill layout.jadewith the layout of the page.
我在 node.js 中有一个网站;要创建一个页面,比如说mypage我注意到我需要创建一个layout.jade和mypage.jade文件。如果我把代码放在mypage.jade里面是不显示的,所以首先我要填充layout.jade页面的布局。
My question is, how do I reference inside layout.jadethat I would like to load the content of mypage.jadein a certain container, for example? Can I have different pages with the same layout? How can I do this?
我的问题是,layout.jade例如,我如何引用我想加载mypage.jade某个容器中的内容?我可以有具有相同布局的不同页面吗?我怎样才能做到这一点?
Thanks
谢谢
回答by alessioalex
http://expressjs.com/guide.html#view-rendering
http://expressjs.com/guide.html#view-rendering
If you don't want to use layouts you can disable them globally:
如果您不想使用布局,您可以全局禁用它们:
app.set('view options', {
layout: false
});
If you don't do that layouts are enabled by default and Express searches for the standard layout in your_view_folder/layout.jade
如果您不这样做,默认情况下会启用布局,并且 Express 在 your_view_folder/layout.jade 中搜索标准布局
You can specify a separate layout for each route though:
您可以为每条路线指定单独的布局:
res.render('page', { layout: 'mylayout.jade' });
// you can omit .jade if you set the view engine to jade
Here's how your layout file could be:
这是您的布局文件的方式:
doctype html
html(lang="en")
head
title Testing 123
body
div!= body
Note that body will be taken from "mypage.jade".
请注意,正文将取自“mypage.jade”。
Edit:
编辑:
Here's a real example in an application:
这是应用程序中的一个真实示例:
The application file (containing routes and configs):
https://github.com/alexyoung/nodepad/blob/master/app.js
应用程序文件(包含路由和配置):https:
//github.com/alexyoung/nodepad/blob/master/app.js
The layout file:
https://github.com/alexyoung/nodepad/blob/master/views/layout.jade
布局文件:https:
//github.com/alexyoung/nodepad/blob/master/views/layout.jade
回答by Scott Rickman
A little late to the party but I struggled to find the answer initially... In layout.jade
聚会有点晚,但我最初很难找到答案......在 layout.jade 中
doctype html
html(lang="en")
head
body
h1 Hello World
block myblock
Then in index.jade
然后在 index.jade
extends layout
block myblock
p Jade is cool
Will render
会渲染
<!DOCTYPE html>
<html lang="en">
<head>
<body>
<h1>Hello World</h1>
<p>Jade is cool</p>
</body>
</html>
回答by rolin.tencent.Shenzhen
In express 3.x
在快递 3.x 中
Use Jade blocks, not layouts
使用 Jade 块,而不是布局
回答by James Mallon
I know an approach that has given me the best results, even with angular (to substitute angular-route/ng-view)
我知道一种方法可以给我最好的结果,即使是 angular(替代 angular-route/ng-view)
First of all will be necessary to install express-layout:
首先需要安装express-layout:
npm install --save express-layout
After that, express will search for layout.jade file inside your views/folder. So, inside of that you can use:
之后,express 将在您的views/文件夹中搜索 layout.jade 文件。因此,在其中您可以使用:
views/layout.jade
意见/layout.jade
html
head
meta(charset='utf-8')
title= title
body
div!= body
views/home.jade
意见/home.jade
h1 Welcome aboard, matey!
server.js
服务器.js
var express = require('express'),
layout = require('express-layout');
var app = express();
app.get('/', function(req, res) {
res.render('home', {
title: 'Welcome!'
});
By default express will search for layout.jade in your views/folder, but you can change the default, by using (yes, it is not necessary to write .jade extension):
默认 express 会在您的views/文件夹中搜索 layout.jade ,但您可以更改默认值,使用(是的,不需要编写 .jade 扩展名):
app.set('layout', 'default');
After that express will use views/default.jade as the default layout.
之后 express 将使用 views/default.jade 作为默认布局。
Also if you want to use a different layout in a particular page, you can use:
此外,如果您想在特定页面中使用不同的布局,您可以使用:
app.get('/', function(req, res) {
res.render('home', {
layout: 'login',
title: 'Welcome!'
});
Here express will render login.jade as the layout.
这里 express 将渲染 login.jade 作为布局。
I suppose that you are using Jade as the default view engine, and don't change the default folder for views.
我想您使用 Jade 作为默认视图引擎,并且不要更改视图的默认文件夹。
Here is the express-layout doc.

