Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda