admin管理员组文章数量:1435859
I have a body that I want to connect to the main template. But unfortunately <%- body %>
is not working, and returning undefined
. I've tried include()
, include file.ejs
, directly path. However, that still didn't work, so I'm not sure what to do. Here's my code:
> app.js
var path = require('path');
var express = require('express');
var app = express();
var expressLayouts = require('express-ejs-layouts');
console.clear();
// EJS //
app.set("view engine", "ejs");
app.use(expressLayouts);
app.set("views", path.join(__dirname, "/mvc/views"));
// SRC FOLDER PERMISSION //
app.use('/src', express.static(path.join(__dirname, 'src')));
// MODULES //
var controller = require(path.join(__dirname, "/mvc/controller/controller"));
// USING CONTROLLER MODULE //
app.use("/", controller);
app.listen(8080);
> mvc/controller/controller.js
var express = require('express');
var router = express.Router();
module.exports.index = function(req, res) {
res.render("template");
}
router.get("/", module.exports.index);
module.exports = router;
> mvc/views/template.ejs
<body>
<div class="container-fluid px-0 h-100">
<%-body%>
</div>
</body>
> mvc/views/home.ejs
<nav class="h-100 d-flex justify-content-center align-items-center text-center">
<div class="line w-100">
<span class="d-block"></span>
<span class="d-block"></span>
</div>
</nav>
> result
ReferenceError: C:\laragon\www\galeri-uygulamasi\mvc\views\template.ejs:18
16| <body>
17| <div class="container-fluid px-0 h-100">
>> 18| <%-body%>
19| </div>
20| </body>
21|
body is not defined
at eval (C:\laragon\www\galeri-uygulamasi\mvc\views\template.ejs:10:16)
at template (C:\laragon\www\galeri-uygulamasi\node_modules\ejs\lib\ejs.js:691:17)
at tryHandleCache (C:\laragon\www\galeri-uygulamasi\node_modules\ejs\lib\ejs.js:272:36)
at View.exports.renderFile [as engine] (C:\laragon\www\galeri-uygulamasi\node_modules\ejs\lib\ejs.js:489:10)
at View.render (C:\laragon\www\galeri-uygulamasi\node_modules\express\lib\view.js:135:8)
at tryRender (C:\laragon\www\galeri-uygulamasi\node_modules\express\lib\application.js:640:10)
at Function.render (C:\laragon\www\galeri-uygulamasi\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\laragon\www\galeri-uygulamasi\node_modules\express\lib\response.js:1012:7)
at ServerResponse.res.render (C:\laragon\www\galeri-uygulamasi\node_modules\express-ejs-layouts\lib\express-layouts.js:77:18)
at module.exports.index (C:\laragon\www\galeri-uygulamasi\mvc\controller\controller.js:5:9)
Does anyone know what's going wrong? Any help would definitely be appreciated. Thanks in advance!
I have a body that I want to connect to the main template. But unfortunately <%- body %>
is not working, and returning undefined
. I've tried include()
, include file.ejs
, directly path. However, that still didn't work, so I'm not sure what to do. Here's my code:
> app.js
var path = require('path');
var express = require('express');
var app = express();
var expressLayouts = require('express-ejs-layouts');
console.clear();
// EJS //
app.set("view engine", "ejs");
app.use(expressLayouts);
app.set("views", path.join(__dirname, "/mvc/views"));
// SRC FOLDER PERMISSION //
app.use('/src', express.static(path.join(__dirname, 'src')));
// MODULES //
var controller = require(path.join(__dirname, "/mvc/controller/controller"));
// USING CONTROLLER MODULE //
app.use("/", controller);
app.listen(8080);
> mvc/controller/controller.js
var express = require('express');
var router = express.Router();
module.exports.index = function(req, res) {
res.render("template");
}
router.get("/", module.exports.index);
module.exports = router;
> mvc/views/template.ejs
<body>
<div class="container-fluid px-0 h-100">
<%-body%>
</div>
</body>
> mvc/views/home.ejs
<nav class="h-100 d-flex justify-content-center align-items-center text-center">
<div class="line w-100">
<span class="d-block"></span>
<span class="d-block"></span>
</div>
</nav>
> result
ReferenceError: C:\laragon\www\galeri-uygulamasi\mvc\views\template.ejs:18
16| <body>
17| <div class="container-fluid px-0 h-100">
>> 18| <%-body%>
19| </div>
20| </body>
21|
body is not defined
at eval (C:\laragon\www\galeri-uygulamasi\mvc\views\template.ejs:10:16)
at template (C:\laragon\www\galeri-uygulamasi\node_modules\ejs\lib\ejs.js:691:17)
at tryHandleCache (C:\laragon\www\galeri-uygulamasi\node_modules\ejs\lib\ejs.js:272:36)
at View.exports.renderFile [as engine] (C:\laragon\www\galeri-uygulamasi\node_modules\ejs\lib\ejs.js:489:10)
at View.render (C:\laragon\www\galeri-uygulamasi\node_modules\express\lib\view.js:135:8)
at tryRender (C:\laragon\www\galeri-uygulamasi\node_modules\express\lib\application.js:640:10)
at Function.render (C:\laragon\www\galeri-uygulamasi\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\laragon\www\galeri-uygulamasi\node_modules\express\lib\response.js:1012:7)
at ServerResponse.res.render (C:\laragon\www\galeri-uygulamasi\node_modules\express-ejs-layouts\lib\express-layouts.js:77:18)
at module.exports.index (C:\laragon\www\galeri-uygulamasi\mvc\controller\controller.js:5:9)
Does anyone know what's going wrong? Any help would definitely be appreciated. Thanks in advance!
Share Improve this question edited Aug 23, 2020 at 7:07 Lioness100 8,4226 gold badges20 silver badges50 bronze badges asked Aug 22, 2020 at 20:54 user9955485user9955485 2-
You need
res.render('home')
and notres.render('template')
– CherryDT Commented Aug 22, 2020 at 22:09 - it did not work but i tried this and worked: res.render('layout'), because module wants a file named 'layout' from me. – user9955485 Commented Aug 23, 2020 at 7:09
3 Answers
Reset to default 3You aren't passing a body
param into your template
res.render("template", { body: "Some text" })
I have a better solution for you. Try this :
First change the 'template.ejs' file name to 'layout.ejs'. Because express-ejs-layouts will look for layout.ejs inside youre views folder.
Inside home.ejs add <%- contentFor('body') %> at top of youre code.
Now you can render youre home.ejs with it's layout.
// >mvc/controller/controller.js module.exports.index = function(req, res) { res.render("home"); }
I hope I could help
本文标签:
javascriptejs lt body gt body is not definedStack Overflow
版权声明:本文标题:javascript - ejs <%- body %> body is not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1745675373a2669820.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论