2014-02-11 3 views
0

Это часть моего JSON файлаДоступ к более глубоких объектов с Mustache.JS

{ 
    "topic": { 
    "topicTitle": "Living things and the environment", 
    "subTopics": [ 
    { 
    "subTopicTitle": "Identifying, namimg and classifying", 
    "subTopicShortName": "identifyingNamingClassifying", 
    "subTopicDescription": "About one and a half million kinds of organisms have been discovered. How do we identify, name and classify them?", 
    "subTopicQuestions": [ 
     { 
     "question": "Use the key to identify the plants a to e in picture 1.", 
     "subTopicQuestionAssetLinks": [ 
      "href://file/filename/image1.jpeg" 
     ], 
     "subTopicQuestionAssetCaptions": [ 
      "Caption 1" 
     ] 
     }, 

И это мой Javascript

<script> 
    $.getJSON('data.json', function(data1) { 
     intro_template = "<h1> {{topic.topicTitle}} </h1> <h2> {{topic.subTopics}} </h2>"; 
     html = Mustache.to_html(intro_template, data1); 
        $('.intro_template_container').html(html); 
    });  
</script> 

Н1 отлично работает отображение «живых существ и окружающей среды»

Beween теги H2 Я хотел бы показать «subTopicTitle» из «идентификации, названия и классификации»

Как это возможно?

ответ

1

Поскольку subTopics является массивом, то вам придется использовать секцию усов:

<h1>{{topic.topicTitle}}</h1> 
{{#topic.subTopics}} 
    <h2>{{subTopicTitle}}</h2> 
{{/topic.subTopics}} 
Смежные вопросы