问题
I have a pug template inside my views folder under views/administration/assets/fixed-assets.pug
My default.pug
which the fixed-assets.pug
extends from is in my root views folder.
When I try to render the fixed-assets.pug
view it looks for the default.pug
inside the views/administration/assets/
directory rather than the views directory itself
Everything works fine if I take the fixed-assets.pug
and place it in the views directory instead of the views/administration/assets/
directory and update the route accordingly.
How can I tell express to look for the default.pug
in the views directory and the fixed-assets.pug
in the views/administration/assets/
directory?
Here is my route
var express = require('express');
var secured = require('../lib/middleware/secured');
var router = express.Router();
/* GET fixed-assets page. */
router.get('/administration/fixed-assets', secured(), function(req, res, next) {
res.render('administration/assets/fixed-assets', {
title: 'Fixed Assets'
});
});
module.exports = router;
Here is my views/administration/assets/fixed-assets.pug
extends default.pug
block scripts
if !starter
script(src='/js/main.js')
block view
.animated.fadeIn
h1 Fixed Assets
and this is the error I'm getting
ENOENT: no such file or directory, open '/usr/src/app/views/administration/assets/default.pug' at /usr/src/app/views/administration/assets/fixed-assets.pug line 1
Thanks for your help!
回答1:
In the docs for Includes it says:
If the path is absolute (e.g., include /root.pug), it is resolved by prepending options.basedir. Otherwise, paths are resolved relative to the current file being compiled.
The explanation for that is in the API Reference:
basedir: string The root directory of all absolute inclusion.
You can implement basedir globally like this in your main app.js/server.js file:
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.locals.basedir = path.join(__dirname, 'views');
来源:https://stackoverflow.com/questions/54382375/how-to-use-folders-in-views-with-express-app