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
  • 17,796
  • 4
  • 32
  • 52
Allen
  • 3,601
  • 10
  • 40
  • 59

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
  • 2
  • 13
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
  • 6,096
  • 2
  • 25
  • 18
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
  • 783
  • 5
  • 10