Defining all of your variables inside your templates would be pretty limiting, so there are a few ways in which we can send data from external sources to templates.
The easiest way to send data to the Jade compiler is by just providing it in a JSON object that gets passed to the compiler as an argument, as shown in the following steps:
In
file.jade
, enter the following:p= my_content
Run the following command in the terminal:
jade file.jade --obj '{"my_content":"this text is coming through the terminal"}'
We can see the result in
file.html
:<p>this text is coming through the terminal</p>
Sending variables to Jade programmatically is a bit harder, but offers more flexibility, such as being able to render within the browser. So, using the same initial file (file.jade
) perform the following steps:
Run the following command in the terminal:
jade file.jade --no-debug --client
And we can see the following result in
file.js
...