0

If I have a module that returns runs this:

res.render('index', {
    title:'Hello World'
});

I can access the title in jade by using #{title}.

How would I access it in separate .js javascript file included in the jade file?

3 Answers 3

2

To access a jade variable in an external JavaScript file, the variable must be declared in a script tag before you link to the relevant JavaScript file.

For example, if you have a script.js file that needs to access the title variable in jade, you could use the following code in your jade file:

script.
    var jadeType = #{type}
script(type='text/javascript', src='./script.js')

The jade variable type can now be accessed in script.js using the JavaScript variable jadeType.

Hope this helps, if anyone is still looking for an answer to this question.

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

Comments

0

I'm not familiar with jade, but assuming you mean another javascript file included on the rendered page, then you can probably do something like:

<script type="text/javascript">
  var title = '#{title}';
</script>

Comments

0

You can't reference those thingie in external js files, included in your jade template. Please, have a look here: Accessing Express.js local variables in client side JavaScript

Comments

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.