Express.js View “globals”

后端 未结 7 1694
太阳男子
太阳男子 2020-12-24 01:16

I\'m using Express.js (on Node.js) and I know that you can render a view with custom data via the \"locals\" parameter. (res.render(\"template\", { locals: { foo: \"ba

7条回答
  •  庸人自扰
    2020-12-24 01:39

    A real-world example of using view options as the author mentioned:

    var app = express.createServer();
    
    app.configure(function() {
      app.set('views', path.join(__dirname, '..', 'views'));
      app.set('view engine', 'jade');
      app.set('view options', {
        assetVersion: 1
      });
    

    And then in my layout.jade (base template for the app in my case):

    link(rel='stylesheet', href='/static/css/' + assetVersion + '/style.css')
    script(src='/static/js/' + assetVersion + '/script.js')
    

    With this little trick, I only have to update the assetVersion variable one place to make sure that my assets aren’t cached in Varnish or other places.

提交回复
热议问题