William Owen
William Owen

Reputation: 445

Listing files in a directory within a Jade template

I am currently using the Jade HMTL templating language (used in conjunction with CodeKit) to generate my site. One of the pages of my site needs to be an index of all the pages within specific directory. Rather than manually keep this updated I would quite like to have the generated automatically.

Is there a way within my Jade template to create a list of the all the files in a geven directory? Is it possible to embed some script that could do this?

Upvotes: 1

Views: 2353

Answers (1)

redhotvengeance
redhotvengeance

Reputation: 27866

Not exclusively with Jade, but you can pass Jade that info from Node.js.

This example assumes you are using Jade with ExpressJS, but you can adapt it as necessary:

Node.js:

app.get('/', function(req, res) {
  fs.readdir(path, function(err, data){
    res.render('index', {"files": data});
  });
});

Jade (index.jade):

ul
  for file in files
    li
      p= file

Upvotes: 3

Related Questions