Home > Back-end >  Ejs doesn't render ejs file
Ejs doesn't render ejs file

Time:03-08

I want to make a queue system for to create pdf file. I created a node server and used Express framework. Also I used rabbitmq for the queue system. I set view engine ejs

app.set('view engine', 'ejs');
app.use(Express.static(__dirname   "/views"));

My folder structure is

consumers
   --consumer_report.js
views
   report.ejs
   report
       --environment.ejs
       --consultans.ejs
       --map.ejs

When a user wants to create a pdf, I redirect it to the queue. The queue is calculating some datas. After calculating I'm using render ejs file.

createReport(msg.user, msg.reportID, msg.type, msg.packetName, (err, data) => {
    data.packet = msg.packetName;

    let dirUrl = __dirname   "/../views/report.ejs";
    let opts = {
        async: true
    }

    ejs.renderFile(dirUrl, data, opts, (err, html) => {
        if (err) return console.error(err);
        console.log("html", html);
    });
});

Report Ejs

<!DOCTYPE html>
<html lang="en" style="zoom:0.75;">
<head>
    <meta charset="UTF-8">
    <title>Report</title>
    <link rel="stylesheet" href="<%= host %>/css/rp.css">
    <script src="<%= host %>/js/jquery3.2.1.min.js"></script>
    <script src="<%= host %>/js/highcharts.js"></script>
    <script src="<%= host %>/js/rp.js"></script>
</head>
<body>
<% for(let i of index){brackets = i.i_page; title= i.name %>
<% if(brackets=="environment") {%> <%- include report/environment.ejs %> <% } %>
<% if(brackets=="consultant") {%> <%- include report/consultants.ejs %> <% } %>
<% if(brackets=="map") {%> <%- include report/map.ejs %> <% } %>
<% } %>
</body>
</html>

When render the ejs file I get this error

Error SyntaxError: missing ) after argument list in /home/aaa/Desktop/projects/report/consumers/../views/report.ejs while compiling ejs

I couldn't find the error. Where is my mistake?

CodePudding user response:

you need to change include syntax to include(path), as per docs:

NOTE: Include preprocessor directives (<% include user/show %>) are not supported in v3.0 . https://github.com/mde/ejs

try this:

<!DOCTYPE html>
<html lang="en" style="zoom:0.75;">
<head>
    <meta charset="UTF-8">
    <title>Report</title>
    <link rel="stylesheet" href="<%= host %>/css/rp.css">
    <script src="<%= host %>/js/jquery3.2.1.min.js"></script>
    <script src="<%= host %>/js/highcharts.js"></script>
    <script src="<%= host %>/js/rp.js"></script>
</head>
<body>
<% for(let i of index){brackets = i.i_page; title= i.name %>
<% if(brackets=="environment") {%> <%- include('report/environment.ejs') %> <% } %>
<% if(brackets=="consultant") {%> <%- include('report/consultants.ejs') %> <% } %>
<% if(brackets=="map") {%> <%- include('report/map.ejs') %> <% } %>
<% } %>
</body>
</html>
  • Related