Here's my Server Side code:
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
app.use(express.static('views'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.get('/',function(req,res){
var title = "PERFORMANCE OVERVIEW";
res.render('Main.html',{name:title});
})
Here's my Client Side code(Main.html):
<div class="row">
<div class="sm-col-12">
<h3 contenteditable="true" style="font-family:BentonSans Medium; text-align:center; color:rgb(0,38,99);"><%= name %></h3>
<hr style="border-top: dotted 2px;color:grey" />
</div>
</div>
The output I am getting on Main.html Page is "<%-name%>". Instead, it should print "PERFORMANCE OVERVIEW". What exactly wrong in this code?
Edit:
I forgot to mention that I have also tried other variants like <%= name%> and {{ name }}
. But I am getting the output "<%= name%>" and "{{ name }}"
respectively.
Here's my Server Side code:
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
app.use(express.static('views'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.get('/',function(req,res){
var title = "PERFORMANCE OVERVIEW";
res.render('Main.html',{name:title});
})
Here's my Client Side code(Main.html):
<div class="row">
<div class="sm-col-12">
<h3 contenteditable="true" style="font-family:BentonSans Medium; text-align:center; color:rgb(0,38,99);"><%= name %></h3>
<hr style="border-top: dotted 2px;color:grey" />
</div>
</div>
The output I am getting on Main.html Page is "<%-name%>". Instead, it should print "PERFORMANCE OVERVIEW". What exactly wrong in this code?
Edit:
I forgot to mention that I have also tried other variants like <%= name%> and {{ name }}
. But I am getting the output "<%= name%>" and "{{ name }}"
respectively.
-
Should that not be
<%= name %>
? At a glance it looks like a typo where-
is there instead of=
.-
means unescaped ( I think ), but I doubt you meant that – Neil Lunn Commented Mar 3, 2019 at 6:24 - I have tried that also, but still getting the <%= name%> as output. Not the "PERFORMANCE OVERVIEW" – Harsh kumar Commented Mar 3, 2019 at 11:22
-
Did you get it working? If my answer didn't help can you update your code to show all the code on your
app.js
page? – Lord Elrond Commented Mar 3, 2019 at 17:27
4 Answers
Reset to default 7changing it to <%= name %>
should fix it.
If that doesn't you can try changing app.set('view engine', 'html')
to app.set('view engine', 'ejs');
, then deleting the following 2 lines.
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.get('/',function(req,res){
var title = "PERFORMANCE OVERVIEW" ;
res.render('Main',{name:title}) ;
});
I have always written it this way, so I can't say for sure if you syntax is correct, or if ejs will even work without setting it like this.
Update
Make sure the Main.html
file is in a folder called views
, and rename this file to Main.ejs
.
next change res.render('Main.html',{name:title});
to res.render('main',{name:title});
<%- Outputs the unescaped value into the template
<%= Outputs the value into the template (HTML escaped)
As you are looking to print the value instead use <%=
tag so change <%- name%>
to <%= name%>
The information can be found here-https://ejs.co/#docs
Change <%- name%> to <%= name%> in your Main.html
const express = require('express');
const bodyParser = require('body-parser');
const port = 5000;
const app = express();
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
app.use(express.static('views'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/', function (req, res) {
var title = "PERFORMANCE OVERVIEW";
res.render('Main.html', { name: title });
});
app.listen(port, console.log(`Server is running on port ${port}`))
Try this and it is rendering HTML page and also accessing the variable you pass in it.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743685151a4490026.html
评论列表(0条)