How do I render partials with jade without express.js?

All we need is an easy explanation of the problem, so here it is.

Only info I found was this:

http://forrst.com/posts/Node_js_Jade_Import_Jade_File-CZW

I replicated the suggested folder structure (views/partials) But it didn’t work, as soon as I put

!=partial('header', {})
!=partial('menu', {})

into index.jade, I get a blank screen, the error message I receive from jade is:

ReferenceError: ./views/index.jade:3
1. ‘p index’
2. ”
3. ‘!=partial(\’header’, {})’

partial is not defined

I’d be very grateful for any help ! (I strongly prefer not to use express.js)

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Jade has a command called include. Just use

include _form

given that the filename of the partial is *_form.jade*, and is in the same directory

Method 2

As of August 2012 (possibly earlier) Partials have been removed from Express.

A lot of tutorials are now out of date. It seems that you can replicate much of the partial functionality with include.

Eg.

movies.jade

div(id='movies')
  - each movie in movies
    include movie

movie.jade

h2= movie.title
.description= movie.description

HTH

Method 3

With the latest node/express I get the following movies.jade template to call partials:

div(id='movies')
  - each movie in movies
    !=partial('movie', movie)

where I have movie.jade in the views directory alongside movies.jade.

movies.jade is called from app.js with:

res.render('movies', { movies: [{ title: 'Jaws' }, { title: 'Un Chien Andalou' }] });

Method 4

I think partial rendering is done in express, so you will have to snag that code or write your own.

I have my own helper class for jade rendering with partials that you can use or get some ideas from here, (it’s using Joose and Cactus)

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply