最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - ejs <%- body %> body is not defined - Stack Overflow

matteradmin9PV0评论

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 not res.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
Add a ment  | 

3 Answers 3

Reset to default 3

You aren't passing a body param into your template

res.render("template", { body: "Some text" })

I have a better solution for you. Try this :

  1. First change the 'template.ejs' file name to 'layout.ejs'. Because express-ejs-layouts will look for layout.ejs inside youre views folder.

  2. Inside home.ejs add <%- contentFor('body') %> at top of youre code.

  3. 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

Post a comment

comment list (0)

  1. No comments so far