Node.js - EJS - 包括部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5404830/
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
Node.js - EJS - including a partial
提问by jeffreyveon
I am trying to use Embedded Javascript renderer for node: https://github.com/visionmedia/ejs
我正在尝试为节点使用嵌入式 Javascript 渲染器:https: //github.com/visionmedia/ejs
I would like to know how I can include another view file (partial) inside a .ejs view file.
我想知道如何在 .ejs 视图文件中包含另一个视图文件(部分)。
回答by pkyeck
With Express 3.0:
使用 Express 3.0:
<%- include myview.ejs %>
the path is relative from the caller who includes the file, not from the views directory set with app.set("views", "path/to/views").
该路径是相对于包含该文件的调用者的路径,而不是来自设置为app.set("views", "path/to/views").
(Update: the newest syntax for ejs v3.0.1 is <%- include('myview.ejs') %>)
(更新:ejs v3.0.1 的最新语法是<%- include('myview.ejs') %>)
回答by czerasz
In Express 4.xI used the following to load ejs:
在 Express 中,4.x我使用以下内容加载ejs:
var path = require('path');
// Set the default templating engine to ejs
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// The views/index.ejs exists in the app directory
app.get('/hello', function (req, res) {
res.render('index', {title: 'title'});
});
Then you just need two files to make it work - views/index.ejs:
然后你只需要两个文件就可以让它工作 - views/index.ejs:
<%- include partials/navigation.ejs %>
And the views/partials/navigation.ejs:
和views/partials/navigation.ejs:
<ul><li class="active">...</li>...</ul>
You can also tell Express to use ejsfor html templates:
您还可以告诉 Expressejs用于 html 模板:
var path = require('path');
var EJS = require('ejs');
app.engine('html', EJS.renderFile);
// Set the default templating engine to ejs
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// The views/index.html exists in the app directory
app.get('/hello', function (req, res) {
res.render('index.html', {title: 'title'});
});
Finally you can also use the ejslayout module:
最后,您还可以使用ejs布局模块:
var EJSLayout = require('express-ejs-layouts');
app.use(EJSLayout);
This will use the views/layout.ejsas your layout.
这将views/layout.ejs用作您的布局。
回答by Daniel Ram
As of Express 4.x
从 Express 4.x 开始
app.js
应用程序.js
// above is all your node requires
// view engine setup
app.set('views', path.join(__dirname, 'views')); <-- ./views has all your .ejs files
app.set('view engine', 'ejs');
error.ejs
错误.ejs
<!-- because ejs knows your root directory for views, you can navigate to the ./base directory and select the header.ejs file and include it -->
<% include ./base/header %>
<h1> Other mark up here </h1>
<% include ./base/footer %>
回答by asapxj07
Works with Express 4.x :
适用于 Express 4.x :
The Correct way to include partials in the template according to thisyou should use:
按照正确的方式在模板中包括谐音这个你应该使用:
<%- include('partials/youFileName.ejs') %>.
<%- include('partials/youFileName.ejs') %>.
You are using:
您正在使用:
<% include partials/yourFileName.ejs %>
<% include partials/yourFileName.ejs %>
which is deprecated.
已弃用。
回答by VCD
Express 3.x no longer support partial. According to the post ejs 'partial is not defined', you can use "include" keyword in EJS to replace the removed partial functionality.
Express 3.x 不再支持部分。根据帖子ejs 'partial is not defined',您可以在 EJS 中使用“include”关键字来替换已删除的部分功能。
回答by Enividu Indonesia
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<form method="post" class="mt-3">
<div class="form-group col-md-4">
<input type="text" class="form-control form-control-lg" id="plantName" name="plantName" placeholder="plantName">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control form-control-lg" id="price" name="price" placeholder="price">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control form-control-lg" id="harvestTime" name="harvestTime" placeholder="time to harvest">
</div>
<button type="submit" class="btn btn-primary btn-lg col-md-4">Submit</button>
</form>
<form method="post">
<table class="table table-striped table-responsive-md">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">FarmName</th>
<th scope="col">Player Name</th>
<th scope="col">Birthday Date</th>
<th scope="col">Money</th>
<th scope="col">Day Played</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<%for (let i = 0; i < farms.length; i++) {%>
<tr>
<td><%= farms[i]['id'] %></td>
<td><%= farms[i]['farmName'] %></td>
<td><%= farms[i]['playerName'] %></td>
<td><%= farms[i]['birthDayDate'] %></td>
<td><%= farms[i]['money'] %></td>
<td><%= farms[i]['dayPlayed'] %></td>
<td><a href="<%=`/farms/${farms[i]['id']}`%>">Look at Farm</a></td>
</tr>
<%}%>
</table>
</form>
回答by Clairton Luz
In oficial documentation https://github.com/mde/ejs#includesshow that includes works like that:
在官方文档https://github.com/mde/ejs#includes 中显示包括这样的作品:
<%- include('../partials/head') %>
回答by jeffreyveon
EJS by itself currently does not allow view partials. Express does.
EJS 本身目前不允许视图部分。快递可以。

