Import Javascript object into jade

I've read and re-read the jade links and a whole bunch of questions, but I can't figure it out and I think it should be very simple.

So, all I want to do is have an object in a JavaScript file and import it into a jade file so that I can use the data from the object when creating my html page.

eg:

This will be in the JS file:

 var obj = {
  firstName: "bob",
  lastName: "smith",
  age: 109

};

      

And in my jade, I want to do this:

h1 #{obj.firstName}, #{obj.lastName}
h2= obj.age

      

and etc.

This is a simple example. Any help whatsoever would be much appreciated.

I would just create an object in my jade, but I want the object to be formatted where each item is on a separate line (for readability) and jade does not currently support .

In this regard, someone mentioned a solution that I didn't understand at all: "So, I did what I wanted by passing the array to grunt-contrib-jade instead of putting it directly into the template. Also let me get rid of grunt-gray "

I am using codekit to compile my jade to static html, not Node.JS.

+3


source to share


1 answer


This can be accomplished if you translate your object to JSON format.

From the docs to command line arguments :
-O, --obj <path|str> JavaScript options object or JSON file containing it

Example:

user.json

{
  "firstName": "bob",
  "lastName": "smith",
  "age": 109
}

      



And you should compile your template like this:

$ jade myTemplate.jade --obj user.json

      

or if you are using Gulp with gulp-jade

:

.pipe(jade({
  locals: require('user.json')
}))

      

+1


source







All Articles