我在将 html 片段包含到我的 index.html 中时遇到一些问题。我试图遵循 ejs 文档,但我似乎无法使其工作。
目录结构:
project
-public
--assets
---css
---images
---js
--Index
---index.html + index.css and index.js
--someOtherPageFolder
-views
--partials
---partials1.ejs
--index.ejs
--layout.ejs
-server.js
Run Code Online (Sandbox Code Playgroud)
这是我的 server.js(新):
var express = require("express");
var partials = require("express-partials");
var http = require('http');
var path = require('path');
var app = express();
app.get("/", function(req, res) {
res.redirect("index");
});
app.configure(function(){
app.set('port', process.env.PORT || 8888);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(partials());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
}); …Run Code Online (Sandbox Code Playgroud) 我有一个复选框,当按下该复选框时,将调用执行 GET 请求的函数。根据选择,我想在同一页面上显示额外的复选框。目前这是我到目前为止的代码:
客户端
function selectedHouse(house)
{
if(house.checked){
$.get('/', {data: house.value});
}
}
Run Code Online (Sandbox Code Playgroud)
服务器端
var routing = function (nav, houses) {
router.route('/')
.get(function (req, res) {
var rooms = [];
rooms = getRooms(req.query.data);
console.log(rooms);
res.render('index', {
title: 'Independent cleaner',
nav: nav,
houses: houses,
roomsForHouse: rooms
});
});
return router;
};
Run Code Online (Sandbox Code Playgroud)
页面第一次加载时,会加载正确的标题、导航和房屋。当该函数在客户端执行时,我取回房屋的相关房间,并尝试填充我在视图上显示的 roomsForHouse 变量。
问题是视图不渲染 roomsForHouse 变量。因此,一旦页面加载,就会调用 GET 请求,并在函数执行时再次调用。这能实现吗?
使用静态站点生成器Hexo,featured我需要显示在登陆页面 (index.ejs) 上标记的所有帖子。
标签在各个帖子文件中定义:
\n\ntitle: Lorem\ntags: \n- featured\n- web\nRun Code Online (Sandbox Code Playgroud)\n\n迭代所有帖子的代码如下所示:
\n\n<% page.posts.each(function(item){ %>\n // TODO: Only insert post if it contains tag \'featured\'\n <%- partial(\'_partial/project-excerpt\', {item: item}) %>\n<% }); %>\nRun Code Online (Sandbox Code Playgroud)\n\n该功能is_tag只能在当前页面使用。所需的功能是if page.is_tag(\'featured\') \xe2\x80\xa6.
我们如何在模板中转义 ejs 标签本身?
在 sails 中,我想将以下代码放入 layout.ejs 中
<!--STYLES-->
<% if (typeof head_css != 'undefined') { %>
<%= head_css %>
<% } else { %>
<% if (typeof head_css_before != 'undefined') { %>
<%= head_css_before %>
<% } %>
<link rel="stylesheet" href="/styles/importer.css">
<% if (typeof head_css_after != 'undefined') { %>
<%= head_css_after %>
<% } %>
<% } %>
<!--STYLES END-->
Run Code Online (Sandbox Code Playgroud)
<!--STYLES-->但默认情况下,和中的所有内容<!--STYLES END-->都会被此模板替换:
<link rel="stylesheet" href="%s">
Run Code Online (Sandbox Code Playgroud)
如在sails-linker.js.
因此,我不想放置模板,而是<link rel="stylesheet" href="%s">放置一些更复杂的东西,而不是渲染时的结果变成:
<!--STYLES-->
<% if …Run Code Online (Sandbox Code Playgroud) 我希望以下控制器在响应时返回一个 html 字符串。目前正在对控制器中的字符串进行硬编码,我认为这不是正确的方法。
router.get('/', function(req, res) {
var employeeDetails; // JSON File Containing Details
// I need ejs to build the page using employeeDetails and store that as a
// string and return this string as the response
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的网站创建一个动态站点地图,它有很多经常更改的页面。需要从 www.mywebsite.com/sitemap.xml 访问站点地图
我当前的尝试是查询所有页面的数据库,获取每个页面的 url 并将其传递给一个 EJS 模板,该模板创建看起来像 XML 的内容。
我这里有两个问题
我意识到还有其他选项可以使用“express-sitemap”等模块创建站点地图,但我找不到任何易于理解的(我是新手)文档,这似乎是一个好方法对我做事
你好
当我尝试渲染 EJS 页面时,我收到此错误...我想错误在我这边,所以请您帮我调试一下好吗?
错误
TypeError: View is not a constructor
at Function.render (/home/bot_king/nodelearn/project/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/home/bot_king/nodelearn/project/node_modules/express/lib/response.js:1008:7)
at /home/bot_king/nodelearn/project/app.js:16:9
at Layer.handle [as handle_request] (/home/bot_king/nodelearn/project/node_modules/express/lib/router/layer.js:95:5)
at next (/home/bot_king/nodelearn/project/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/bot_king/nodelearn/project/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/bot_king/nodelearn/project/node_modules/express/lib/router/layer.js:95:5)
at /home/bot_king/nodelearn/project/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/bot_king/nodelearn/project/node_modules/express/lib/router/index.js:335:12)
at next (/home/bot_king/nodelearn/project/node_modules/express/lib/router/index.js:275:10)
Run Code Online (Sandbox Code Playgroud)
代码
var express = require("express");
var app = express();
var port = 8080;
var ejs = require('ejs');
app.use(express.static("src/view"));
app.set("view", "./src/view");
app.set("view engine", "ejs");
app.use(express.static("public"));
app.listen(port, function(err){
console.log("The server is running on port " + port);
});
app.get("/", …Run Code Online (Sandbox Code Playgroud) 所以我已经使用 React 有一段时间了,但是我需要在我当前的项目中使用 ejs。我想做的是返回各种radio元素。我试着用这个代码来做:
<% const renderRadios = (value, name) => (
`<label for="eventRate${value}" class="col-sm-2 col-form-label">1</label>
<input type="radio" id="eventRate${value}" class="form-control" name="${name}" value="${value}" required>`
) %>
<% for (let i = 1; i <= 5; i++) { %>
<%= renderRadios(i, 'eventRate'); %>
<% } %>
Run Code Online (Sandbox Code Playgroud)
我认为这解释了我的意图。这就是这段代码正在做的事情:
我的意思是它按照我的吩咐去做。但我希望浏览器将其解析为 HTML。任何想法如何将这些元素呈现为 HTML?
我想从我的 ejs 文件中调用我的一些异步函数。
就像我将此功能设置为我的 app.locals.someFunction
async someFunction(param){
let data = await someAsyncStuff();
// & other things
return data;
}
Run Code Online (Sandbox Code Playgroud)
我想在 ejs 文件中使用它,如下所示:
<%
let data = await someFunction()
for(let x of data){%>
<li><%=x%></li>
<%}%>
Run Code Online (Sandbox Code Playgroud)
如果{async:true}作为选项传递,这对于 ejs 是可能的。但是,当我的视图引擎设置如下所示时,我到底应该在哪里传递它?
//view engine setup
app.engine ('.html', ejs.renderFile);
app.set ('view engine', 'html');
Run Code Online (Sandbox Code Playgroud) 我在 VSCode 上遇到 EJS 文件的问题,每次我保存文件时,格式化程序都会应用错误的缩进。
在每个 EJS 标识符下方,下一行向右移动,缺少正确的垂直对齐。
我为 *.ejs 使用 HTML 格式并安装了来自 DigitalBrainstem 的 EJS 扩展
<% if(locals.metadata && locals.metadata.length>0) { %>
<% var supplier=metadata[0] %>
<% } %>
<main class="site-content">
Run Code Online (Sandbox Code Playgroud)
下图中的另一个例子

您可以在两个示例中看到,它看起来像一个梯子,每一行都自动向右缩进,并且无法保持打开/关闭代码块对齐。
ejs ×10
express ×7
node.js ×7
javascript ×4
html ×2
async-await ×1
formatting ×1
generator ×1
hexo ×1
indentation ×1
mongodb ×1
render ×1
sitemap.xml ×1
static-site ×1
typeerror ×1