使用Mustache.JS访问更深层的对象

时间:2014-02-11 10:41:20

标签: javascript mustache

这是我的JSON文件的一部分

{
  "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>

h1完美地展示了“生物与环境”

在H2标签之间我想展示“识别,命名和分类”的“subTopicTitle”

这怎么可能?

1 个答案:

答案 0 :(得分:1)

由于subTopics是一个数组,你必须使用胡子部分:

<h1>{{topic.topicTitle}}</h1>
{{#topic.subTopics}}
    <h2>{{subTopicTitle}}</h2>
{{/topic.subTopics}}
相关问题