2013-05-09 124 views
0

我有這樣的玉snippt:如何將複雜的對象數組傳遞給Jade模板?

div.comment_list 
    - var clist = comment_list 
    each comment in clist 
     div.comment 
      div.comment_detail 
       span.commentator comment.commentator 
       span.comment_time comment.comment_time 
       span.comment_content comment.comment_content 

comment_list是我進入玉模板。

的問題是: 每個數組元素comment由複合對象,因爲您看見其中,, 具有commentatorcomment_time,和comment_content

然而,玉不能識別這個事實,它只是輸出:comment.commentatorcomment.comment_time,comment.comment_content vebatim。

那麼如何解決這個問題呢?

回答

4

你不需要- var一切無論是。例如。

div.comment_list 
    each comment in comment_list 
     div.comment_detail 
      span.commentator #{comment.commentator} 
      span.comment_time #{comment.comment_time} 
      span.comment_content #{comment.comment_content} 

如果您要輸出單個變量,則不需要#{}包裝。只有當你在那裏混合純文本時才需要它。

div.comment_list 
    each comment in comment_list 
     div.comment_detail 
      span.commentator= comment.commentator 
      span.comment_time= comment.comment_time 
      span.comment_content= comment.comment_content 

而且div的是多餘的 - 你可以簡單地我們.class#id或兩者兼而有之。顯然,如果你沒有設置課程或ID,你需要在那裏有div這個詞。

.comment_list 
    each comment in comment_list 
     .comment_detail 
      span.commentator= comment.commentator 
      span.comment_time= comment.comment_time 
      span.comment_content= comment.comment_content 
+0

謝謝!好答案! – larmbr 2013-05-11 02:45:00

-1

我已經想通了。

讓代碼來說話:

div.comment_list 
    - var clist = comment_list 
    each comment in clist 
     - var commentator = comment.commentator 
     - var comment_time = comment.comment_time 
     - var comment_content = comment.comment_content 
     div.comment_detail 
      span.commentator #{commentator} 
      span.comment_time #{comment_time} 
      span.comment_content #{comment_content} 
相關問題