为Jade布局设置CSSpath
我想在我的快速应用程序中设置我的CSSpath,以便在我的玉石布局中使用这个。 然而,我不知道该怎么做,我尝试使用“app.set('cssPath',__dirname +'/ public / admin / css /')”,但它不工作,因为我不能使用“应用程序。得到()“在我的外部控制器。
我的布局_layout.jade:
!!! 5 html(lang='fr') head meta(charset='UTF-8') link(href='admin/css/screen.css', media='screen, projection', rel='stylesheet', type='text/css') body .container h1 Wellcome to Forest Administrator .space20 block content .clear.space20 script(type='text/javascript', src='admin/js/jquery.js')
我的页面edit.jade:
extends ../_layout block content .block.half.first h2 Add a post
而且我想用这样的东西:
link(href='+myCssPath+', media='screen, projection', rel='stylesheet', type='text/css')
不知道如果我得到你想要做的,但你可以使用
res.locals.cssPath = 'string with the path';
和cssPath将在您的模板中可用。
除此之外,你不需要__dirname +'/ public /。 部分是因为当页面呈现为浏览器/ public /将/
如果你想在你所有的路由中都有这个variables,但是只声明一次,你可以创build一个小的中间件
var express = require('express'); var app = express(); var app.configure(function(){ app.use(express.bodyParser()); app.use(express.methodOverride()); // .. and your other tipical configuration //this small middleware for variables that must available in all paths app.use(function(req, res, next) { res.locals.cssPath = 'path to the css directory'; next(); }); }); //From here your typical route declarations app.get('/', function(req, res) { res.render('someView'); });