node.js - 设置 Jade 布局的 CSS 路径

标签 node.js express pug

我想在我的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/

相关文章:

node.js - firebase deploy 没有找到与模式 "' src/**/*'"匹配的文件

node.js - 将 webview View 设置为窗口大小

Node.js Express,请求 jwt token - 如果请求过期,如何正确获取新的有效 token

javascript - 将div标签变成输入框

javascript - 从一个函数内部的 request.get 返回变量值到另一个 NODEJS 表达式

node.js - model.fetch 与相关模型 bookshelfjs

javascript - 用于检查 CouchDB nodejs nano 中是否存在文档的自定义函数

node.js - MEAN Stack - 仅在某些 REST 方法上使用 jwt 和express-restify-mongoose

express - node.js表达createServer()不是函数

javascript - 如何在jade中进行嵌套迭代?