2017-03-14 92 views
1

我找遍歷數組的對象列表鬍子迭代對象

這裏是我的樣本對象

var Categories = { 
     "communication": [ 
     { 
      "id": "communication_001", 
      "category": "communication", 
      "lastModified": "4 Day ago" 
     }, 
     { 
      "id": "communication_002", 
      "category": "communication", 
      "lastModified": "1 Day ago" 
     } 
     ], 
     "social": [ 
     { 
      "id": "social_001", 
      "category": "social", 
      "lastModified": "2 Day ago" 
     } 
     ], 
     "storage": [ 
     { 
      "id": "storage_001", 
      "category": "storage", 
      "lastModified": "3 Day ago" 
     } 
     ] 
    } 

我在這裏跟鬍子compling

var htmlTemplate= Mustache.render(template, { connectors: Categories }); 

那麼要怎麼做成爲我的編譯模板?

建議我鬍子模板。

回答

1

一個可能的模板:

{{#connectors}} 
     Communication<br> 
    {{#communication}} 
     {{id}} - {{category}} - {{lastModified}}<br> 
    {{/communication}} 
    <br><br>Social<br> 
    {{#social}} 
     {{id}} - {{category}} - {{lastModified}}<br> 
    {{/social}} 
    <br><br>Storage<br> 
    {{#storage}} 
     {{id}} - {{category}} - {{lastModified}}<br> 
    {{/storage}} 
{{/connectors}} 

這會產生這樣的:

Communication 
communication_001 - communication - 4 Day ago 
communication_002 - communication - 1 Day ago 


Social 
social_001 - social - 2 Day ago 


Storage 
storage_001 - storage - 3 Day ago 

在我使用sections這將表現不同,這取決於如果列表是或不是的列表要迭代空:

  • 空列表:該部分不會呈現
  • 非空列表:對列表中的每個元素都將渲染一次這些部分,將該部分的上下文設置爲當前項目,以允許您調用它的屬性並遍歷列表。

但是你可以在模板中做更多的事情,所以你應該檢查documentation