0

I rendered view with one variable and want to use the variable in javascript file included rendered view. I use pug view template

**Server Side.**

res.render('myView', { myval:'pizza'});

**Client Side**

extends ../layout

block content
  script(src="scripts/myscript.js")

**myscript.js**

$(function(){
   alert(myval);
});

or 

**index.pug**

    script(type='text/javascript').
    var val= '#{myval}';
    alert(val);

how to do it? looking forward any help.

1 Answer 1

2

In your view myView you need to render that value in js block.

script(type='text/javascript')
    var myval = '#{myval}';

It will be visible in your script.

Sign up to request clarification or add additional context in comments.

4 Comments

And what about alert? Does it display proper value?
oh everything is ok. I set enctype of form as multipart/form-data.
I changed it as application/x-www-form-urlencoded and it work well. Thanks. Btw, what is difference between '#{myval}' and '!{myval}'?
#{myval} is evaluated & escaped. !{myval} is evaluated and unescaped. So if you want to do myval = '#{myval}' you need to escape string, because if you don't using string with ' will break your code. pugjs.org/language/interpolation.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.