Helmut K.
Helmut K.

Reputation: 121

node.js and ejs rendering issuses

I have a problem with my code and ejs rendering

var express = require("express");
var app = express();
var bodyParser = require('body-parser');
var path = require("path");

//View Engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));


app.use(express.static(__dirname + '/views'));


var users = "John Doe";

app.get('/', function(req, res){
  var title = 'Customers';
  res.render('index', {
    title: title,
    users: users
  });

  console.log(Date() + " accessed Site");
});



app.listen(8080);

console.log("Running at port 8080...");

This seems to be such a simple code, but it does not work. This is my index.ejs

<!DOCTYPE html>
<html lang="de" dir="ltr">
  <head>
    <% include partials/includes %>
    <meta charset="utf-8">
    <title>{%= title %}</title>
  </head>
  <body>
    <% include partials/header %>
    <h1>Alles hat ein Anfang nur die Wurst... wait a second!</h1>
    <h2>{%= users %}</h2>
  </body>
</html>

So the problem is, that my variables title and users dont want to get rendered. I have no idea what I am missing.

Upvotes: 1

Views: 64

Answers (1)

Muhammad Usman
Muhammad Usman

Reputation: 10148

Use

<%= title %> or <%- title %>

to echo the value on page.

In general, we use <%= %> or <%- %> to show sent variables/objects on page

Upvotes: 1

Related Questions