Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Use variable in ejs script tag

I want to use variable (or variables) in ejs file in the script tag ,I also want to pass the rendered file to a function for taking screenshot from ejs file.

But now I have 2 problems :

1.I don't know how to pass the variable in server file to the ejs file and render and use it without app.get... (in express) because it's server side and I want to use html file.

2.I don't know how to use variable in ejs file in the script tag

these are my files :

index.ejs


<div id="tvchart"><% symbol %></div>

<script>
//some codes 
 var symbolD =<%= symbol %>;
fetch(`http://127.0.0.1:9665/fetchAPI?endpoint=https://api.binance.com/api/v3/klines?symbol=${symbolD}&interval=1m&limit=1000`)
</script>

server.js

// set the view engine to ejs
app.set("view engine", "ejs");
const symbol = "EGLDUSDT"

const file = ejs.render("./index.ejs" , symbol);
console.log(file)

So Why my ejs and server file doesn't work?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The second argument to render needs to be an object where the property names are the variables passed to the EJS file.

ejs.render("./index.ejs" , {symbol});

Once you fix that var symbolD =<%= symbol %>; gives you var symbolD =EGLDUSDT; which throws an error because you don't have a variable named EGLDUSDT.

Use JSON.stringify to generate something compatible with JavaScript literal syntax (i.e. with quotes around strings and proper escaping for special characters).

var symbolD = <%= JSON.stringify(symbol) %>;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!