我想在我的express应用程序中设置我的CSS路径,以便在我的jade布局中使用这个路径。但是,我不知道该怎么做,我尝试使用“app.set('cssPath', __dirname+'/public/admin/css/')”,但它不起作用,因为我无法在外部 Controller 中使用“app.get()”。
我的布局_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/将是/
[编辑]如果您希望该变量在所有路由中可用,但只声明一次,您可以创建一个小型中间件,例如
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'); });
关于node.js - 设置 Jade 布局的 CSS 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18206948/