Scott
Scott

Reputation: 13921

Jade include file and pass model data

I'm trying to learn Node/Jade and a few other JS-centric technologies all at once. I'm coming from a .NET MVC background so I'm used to being able to pass my entire model or properties of the model to partial files, which I separate out from my main view for the sake of reuse.

I have this simple route defined:

index.js

router.get('/', function(req, res, next) {
  res.render('index', data);
});

var data = {
    title: 'This is the title',
    posts: [
        {
            firstname: 'Scott'
        },
        {
            firstname: 'John'
        }
    ]
}

index.jade

extends layout

block content
  section
    each p in posts
      include post ????? //How would I pass the "p" object to my include

post.jade

div.post
  p #{firstname}

So my question (also inline within the index.jade sample) is, how do I pass the "p" object within my each loop to the include? If that's not possible, what's the accepted approach with Jade for accomplishing something like this?

Sorry for the rookie question, just getting started with Jade/Express/Node!

Upvotes: 0

Views: 347

Answers (1)

Scott
Scott

Reputation: 13921

Figured it out.

index.jade

extends layout
include post

block content
  section
    each p in posts
      mixin post(p)

post.jade

mixin post(p)
  div.post
    p #{p.firstname}

Upvotes: 2

Related Questions