2012-10-06 38 views
0

我使用express.js和jade。我在玉模板有問題。獲得Jade模板列表中的第一個項目

我mogodb數據就像下面

{ _id: 5040465ce0afabce2b000003, 
    address: 'Sample', 
    fax: [ '22 212', '34 138' ], 
    Branch: true, 
    pic: [ 'photo1.png' ,'photo2.png'], 
    tel: [ '22 980', '22 439' ], 
    lat: 29.674292, 
    Name: 'Branch 1', 
    ATM: false, 
    long: 98.210881 }, 
{ _id: 5040465ce0afabce2b000003, 
    address: 'Loikaw.', 
    fax: [ ], 
    Branch: true, 
    pic: [ ], 
    tel: [], 
    lat: 20.674292, 
    Name: 'Loikaw Branch', 
    ATM: false, 
    long: 98.210881 }, 
從我的玉模板

- obj.forEach(function(item){ 
       tr 
        td 
         img(src="/upload/#{item.pic}",width=200) 
        td #{item.Name} 
        td #{item.address} 
        td #{item.ATM} 
        td #{item.Branch} 
        td #{item.lat} 
        td #{item.long} 

如何使用的pic array第一陣列?

我檢查了doc,https://github.com/visionmedia/jade,它沒有包含數組。他們只顯示迭代和數組。

如果沒有圖片,我想顯示其他圖片。

有沒有可能在Jade模板中完成,或者我需要在js端做?

+1

你嘗試'IMG(SRC = 「/上傳/#{item.pic [0]}」,寬度= 200)'? –

+0

是的。它正在工作。謝謝。 – saturngod

回答

2
- obj.forEach(function(item){ 
       tr 
        td 
         - if (item.pic[0] == undefined) 
          img(src="/images/nopic.png",width=200) 
         - else 
          img(src="/upload/#{item.pic[0]}",width=200) 

謝謝@Ray托爾

相關問題