对节点使用Nunjucks
试图为以下问题找到一个合适的解决方案。目录树sorda如下: app_dir --app.js --public ----stylesheets ------mystyles.css --views ----page.html ----templates ------page_template.html
app_dir --app.js --public ----stylesheets ------mystyles.css --views ----page.html ----templates ------page_template.html
app.use(express.static(path.join(__dirname, 'public')));
将Nunjucks的根目录配置为视图 nunjucks.configure('views', { autoescape: true, express : app, watch: true });
nunjucks.configure('views', { autoescape: true, express : app, watch: true });
当我从page_template中引用css文件时。html,nunjucks(我认为)会根据路由自动创建相对路径,并覆盖静态行为。
例如,当我使用 /stylesheets/mystyles.css page_template上的路径。html,但使用调用扩展它的文件 /:publication/:page 路径,呈现的html为 /:publication/:page/stylesheets/mystyle.css
/stylesheets/mystyles.css
/:publication/:page
/:publication/:page/stylesheets/mystyle.css
我总是可以写一个快速的技巧,根据路径创建CSS和其他资源的相对路径,但这并不是一个特别优雅的解决方案:(非常感谢任何帮助。
我认为这是错误的。Nunjucks不会生成任何路径。 在任何文件夹的模板中( view , view/templates ,等)必须指定文件名 public dir 是根,例如。 /stylesheets/mystyles.css 对于 %app%/public/stylesheets/mystyles.css .
view
view/templates
public dir
%app%/public/stylesheets/mystyles.css
我在中使用子文件夹 看法 用于对模板进行分组,例如。 /macros (存储的宏), /tools (为我的应用程序存储了其他页面)。也可以将其用于路由器,例如。 /user/view.html , user/add.html ...
看法
/macros
/tools
/user/view.html
user/add.html