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?

throrin19
  • 16,986
  • 4
  • 29
  • 50
Allen
  • 3,421
  • 8
  • 38
  • 54

3 Answers3

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.

Jeremy D
  • 197
  • 1
  • 12
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>
Kevin Reilly
  • 5,898
  • 2
  • 23
  • 17
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

Community
  • 1
  • 1
Mr_Mig
  • 785
  • 5
  • 10