I have an app with views that are in directories like so:
$ ftree ./web/views
| |____views
| | |____index.jade
| | |____404.jade
| | |____partials
| | | |____test.index.jade
| | | |____index.jade
| | | |____test
| | | | |____index.jade ****
| | |____layout.jade
| | |____500.jade
I'd like to be able to render the view at /views/partials/test/index.jade when I call /partials/test/index.jade. I have my route set up like so:
exports.partials = (req, res)->
filename = req.params.filename
return unless filename # might want to change this
console.log "rendering partial at #{filename}"
res.render "partials/#{filename}"
and
app.configure(->
app.set('port', process.env.PORT || 3001)
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
...
app.get('/partials/:filename', routes.partials)
This all works fine for partials/index.jade and partials/test.index.jade but it fails (404) for partials/test/index.jade.
Do I have to make a route for each subdirectory of /partials? I'd like express to render any file under /partials, since there will be a few of them. I considered using the static directory instead but it looks like static files won't go through the jade processor middleware.
params cannot contain /. When evaluated the url is tokenized by / to get params.
When you do app.get('/partials/:filename', routes.partials) it matches partials/index.jade because req.params.filename is set to index.jade (only one valid params). But it does not match partials/test/index.jade because two params are required to match.
To achieve what you want you can do this
app.get('/partials/*', routes.partials)
and in routes do:
exports.partials = (req, res)->
filename = req.params
return unless filename # might want to change this
console.log "rendering partial at #{filename}"
res.render "partials/#{filename}"
In above code req.params matches everything after /partials/ so you render the path correctly. So you can even create sub directories inside them