2012-06-01 20 views
0

好的,所以我有一個快速讓我的模板使用擴展/阻止關鍵字的問題。我能夠使索引模板工作得很好;但是,當我嘗試添加第二個模板時,「擴展」似乎不起作用。我已經粘貼了下面的代碼,我會很感激! (作爲參考,我運行明示版本2.5.8和節點0.6.12)玉在express.js中擴展關鍵字問題

app.js:

var express = require('express') 
    , routes = require('./routes'); 

var app = module.exports = express.createServer(); 

// Configuration 

app.configure(function(){ 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'jade'); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(express.cookieParser()); 
    app.use(express.session({ secret: 'your secret here' })); 
    app.use(app.router); 
    app.use(express.static(__dirname + '/public')); 
    app.set('view options', { layout: false, pretty: true }); 
}); 

app.configure('development', function(){ 
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
}); 

app.configure('production', function(){ 
    app.use(express.errorHandler()); 
}); 

// Routes 

app.get('/', routes.index); 
app.get('/login', routes.login); 

app.listen(3000, function(){ 
    console.log("Express server listening on port %d in %s mode", app.address().port,  app.settings.env); 
}); 

路由/ index.js:

exports.index = function(req, res){ 
    res.render('index', { title: 'rm-dash-r' }) 
}; 

exports.login = function (req, res) { 
    res.render('login', { title: 'rm-dash-r' }) 
}; 

在視圖/目錄中有3個文件。 index.jade,main-layout.jade和login.jade。爲了測試,登錄和索引是相同的文件。

主layout.jade:

!!! 
html 
    head 
    title= title 
    link(rel='stylesheet', href='/stylesheets/style.css') 
    body(style='margin: 0 22%;') 
    div.container 
     div.header 
      a(href='/') 
       span rm-dash-r 

     div.stripes 
      span 

     div.nav 
      a(href='/') Blog 
      a(href='/') About 
      a(href='/') Projects 
      div.clearer 
       span 

     div.stripes 
      span 

     div.main 
      div.left 
       div.content 

        block content 
... 

index.jade/login.jade:

extends main-layout 

block content 
    h1 testing 

實質上,路線 '/' 工作沒有任何問題,但登錄路由會只呈現login.jade標記,除了在頁面頂部擴展主佈局。

如果有其他任何有助於瞭解的信息,請讓我知道。

+0

我運行了你的代碼,無法重現你正在談論的錯誤。在內容塊之後的主佈局中是否還有其他內容?如果是,請嘗試刪除它並查看輸出是否不同。 – tUrG0n

+0

之後嘗試刪除代碼,但仍然看到相同的問題。您能夠同時獲得/和/ login路徑以正確運行?你的系統上有什麼版本的express/node/jade?謝謝。 – ebensing

+0

玉:0.21.0 |節點v0.6.18 |表達:2.5.8 ...你有代碼在線託管嗎?如果沒有,我會嘗試在github上創建一個:) – tUrG0n

回答

0

好吧,經過重大的試驗和錯誤,我似乎有它的工作。出於某種原因,在向login.jade的第一行添加一個返回值(即擴展main-layout現在是第二行)之後,這一切似乎都奏效了。

我沒有絲毫的想法,爲什麼會出現這種情況,但是如果有人能說出一些光,我會很有興趣聽到。