How to pass variable from pug template file to a script file?

2.2k Views Asked by At

From express i pass the variable locals to the template engine like this:

res.render("Main.pug",locals);

I can use the variable inside the template engine. But i also want to use it inside the included script file

script(src='/file/Index.js')

i have tried following,

script(src='/file/Index.js') 
  | var myVar = "Hello";

But not working.

1

There are 1 best solutions below

0
Pol.H On

Now its working,

Needs to be above any other script files

script(type="text/javascript"). var myVar ="test" ;

inside Index.js:

alert(myVar)

And if you want to use variable passed from express to script file use:

 script(type="text/javascript").
  var myVar ="#{JSON.stringify(data)}";