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?
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.
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>
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