2012-12-09 26 views
2

這是我的配置文件: 的layout.jade似乎並不奏效。但玉器正在工作。我使用Chrome進行檢查,並確定佈局HTML未加載到頁面中。layout.jade不起作用,爲什麼?

module.exports = function(app, express, mongoose){ 
    var config=this 

    app.configure(function(){ 
     app.set('views',__dirname+'/views') 
     app.set('view engine','jade') 
     app.set('view options', {layout:true}) 

     app.use(express.bodyParser()) 
     app.use(express.methodOverride()) 
     app.use(express.cookieParser()) 
     app.use(express.session({secret: 'topsecret',store: new express.session.MemoryStore})) 
     app.use(express.static(app.path.join(app.application_root,"public"))) 
     app.use(express.errorHandler({dumpExceptions:true,showStack:true})) 
     app.use(express.bodyParser({keepExtensions: true, uploadDir:"./public/uploads"})) 
     app.use(app.router) 
    }) 

    /*DB part:*/ 
    app.mongoose.connect('mongodb://localhost/dio_database') 

    return config 
} 

渲染命令:

app.get('/items/:id',function(req,res){ 
    models.ItemModel.findOne({_id:req.params.id}).exec(function(err,item){ 
     if (!err){ 
      res.render('item.jade',item) 
     } else 
      return console.log(err) 
    }) 
}) 

我layout.jade,很簡單:

!!! 
doctype 5 
html 
    head 
     title "Dio" 
     link(rel='icon', href='favicon.ico', type='image/x-icon') 
     link(rel='shortcut', href='favicon.ico', type='image/x-icon') 
     link(rel="shortcut", href="favicon.ico", type="image/vnd.microsoft.icon") 
     link(rel="icon", href="favicon.ico", type="image/vnd.microsoft.icon") 

     script(src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js") 
     script(src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js") 
     script(src="./javascripts/underscore-min.js") 
     script(src="./javascripts/backbone-min.js") 

     link(rel='stylesheet', href='./css/main.css', type="text/css", media="screen") 
    body 
     div#topbar Dio--where shitty thing happens 
     div#main!= body 
      footer 
       p 
        | Node.js MVC template by XXX 

而以下是我的NPM列表:

├─┬ [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └─┬ [email protected] 
│ └── [email protected] 
├── [email protected] 
├── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
├─┬ [email protected] extraneous 
│ ├── [email protected] 
│ └─┬ [email protected] 
│ └── [email protected] 
└── [email protected] 
+1

OK,我認爲這個問題是新玉不再支持佈局,但支持塊和延伸。我只是想知道這件事很小,幾乎沒有人明確提出它?花了我2個小時弄清楚了。 –

+1

Express3不再支持佈局 –

+0

您的item.jade模板在哪裏?此外,你可以更新你的問題與錯誤輸出,如果有任何?...此外,檢查出你傳遞項目的方式作爲本地變種模板....不應該是{item:item}? ?也許你應該嘗試一個簡單的佈局和項目模板渲染,以測試你的情況...... –

回答

12

其實這個問題的原因很簡單: 快遞3不再支持佈局..但不要傷心。其實快車3開始採用更自然和更簡單的方法,這就是所謂塊/擴展。 基本用法應該是這樣的:

// In layout file: layout.jade 
html 
    head 
     title XXX 
     block scripts 
    body 
     block content 
     block footer 


// in a extended file, for example index.jade: 
extends layout 
block scripts 
    //write javascript part 
block content 
    // write content 
block footer 
    // write the footer 

實際上,它變得更容易,更靈活。很高興終於得到它。但花了我2個多小時。

我只是想知道爲什麼這麼少的人更明確公開提及這一變化。希望這篇文章能夠幫助像我這樣的人。

+1

感謝和快速註釋,以確保您在擴展文件中縮進您的塊代碼,就像@Yitong對評論所做的一樣。 –

+0

希望我可以給你多個投票。這個解決方案和上面的註釋最終幫助我解決了爲什麼我的擴展文件沒有顯示。謝謝! – Kildareflare