我正在尝试使用CoffeeScript,NodeJS和expressJS创建一个简单的布局。我的咖啡脚本是
routes.coffee 我已将路径文件夹重命名为apps,而我的routes.coffee文件位于身份验证文件夹中,即apps\authentication\routes.coffee
routes = (app) ->
app.get "/login", (req , res) ->
res.render "#{__dirname}/views/login",
title: 'Login',
stylesheet: 'login'
module.exports = routes
登录视图我已将路径文件夹重命名为应用程序,并将身份验证文件夹中的登录视图重命名为apps\authentication\views\login.jade
extends ../../../views/layout
block content
form(action='/sessions', method='post')
label
| Username
input(type='text', name='user')
label
| Password
input(type='password', name='password')
input(type='submit', name='Submit')
Layout.jade 此文件位于默认视图文件夹中。我正在使用默认布局
doctype
html
head
title= title
body
#content
h1= title
block content
!= body
app.js
require('coffee-script').register();
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
//var index = require('./routes/index');
//var users = require('./routes/users');
var app = express();
// view engine setup
// app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// app.set('view options', { layout: 'views/layout' });
// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, '/public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '/public')));
// app.use(express.static('public'));
app.set('view options', {
layout: false
});
//app.use('/', index);
//app.use('/users', users);
require('./apps/authentication/routes')(app);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
我在title
中创建了两个变量stylesheet
和routes.coffee
但是,当我尝试在layout.jade
即标题时实现这些变量时,我得到undefined
作为标题中的标题。
有人可以帮忙吗?
感谢。
答案 0 :(得分:1)
我认为您没有正确使用缩进。在您的登录视图中:
extends ../../../views/layout
block content
form(action='/sessions', method='post')
label
| Username
input(type='text', name='user')
label
| Password
input(type='password', name='password')
input(type='submit', name='Submit')
应该是:
extends ../../../views/layout
block content
form(action='/sessions', method='post')
label
| Username
input(type='text', name='user')
label
| Password
input(type='password', name='password')
input(type='submit', name='Submit')