Javascript 咕噜的胡子"u render won"';无法正确呈现HTML文件

Javascript 咕噜的胡子"u render won"';无法正确呈现HTML文件,javascript,gruntjs,mustache,Javascript,Gruntjs,Mustache,我使用布局文件和json文件来呈现HTML文件 下面是Grunt任务: mustache_render: { json_data: { files: [ { data: 'output.json', template: 'test.mustache', dest: 'tmp/hello_json.html' } ] } }

我使用布局文件和json文件来呈现HTML文件

下面是Grunt任务:

mustache_render: {
      json_data: {
        files: [
          {
            data: 'output.json',
            template: 'test.mustache',
            dest: 'tmp/hello_json.html'
          }
        ]
      }
    }
输出JSON如下所示:

[
  {
    "name": "John Doe",
    "age": "30"
  },
  {
   "name": "Alex Len",
    "age": "27"
  },
  {
   "name": "Debbie John",
    "age": "36"
  }

]
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Mustache Sample</title>
</head>
<body>
  <h2>Titles</h2>
  <ul id="talktitles">
    <script id="speakers-template" type="text/template">
    {{#options}}
      <li>{{{name}}}, {{{age}}}</li>
    {{/options}}
  </script>
  </ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Mustache Sample</title>
</head>
<body>
  <h2>Titles</h2>
  <ul id="talktitles">
    <script id="speakers-template" type="text/template">
  </script>
  </ul>
</body>
</html>
test.mustache如下所示:

[
  {
    "name": "John Doe",
    "age": "30"
  },
  {
   "name": "Alex Len",
    "age": "27"
  },
  {
   "name": "Debbie John",
    "age": "36"
  }

]
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Mustache Sample</title>
</head>
<body>
  <h2>Titles</h2>
  <ul id="talktitles">
    <script id="speakers-template" type="text/template">
    {{#options}}
      <li>{{{name}}}, {{{age}}}</li>
    {{/options}}
  </script>
  </ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Mustache Sample</title>
</head>
<body>
  <h2>Titles</h2>
  <ul id="talktitles">
    <script id="speakers-template" type="text/template">
  </script>
  </ul>
</body>
</html>

胡子样本
标题
    {{{#选项}
  • {{{name}}},{{{{age}}
  • {{/options}}
当我运行grunt时,它创建了tmp/hello_json.html文件,但没有填充值。看起来是这样的:

[
  {
    "name": "John Doe",
    "age": "30"
  },
  {
   "name": "Alex Len",
    "age": "27"
  },
  {
   "name": "Debbie John",
    "age": "36"
  }

]
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Mustache Sample</title>
</head>
<body>
  <h2>Titles</h2>
  <ul id="talktitles">
    <script id="speakers-template" type="text/template">
    {{#options}}
      <li>{{{name}}}, {{{age}}}</li>
    {{/options}}
  </script>
  </ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Mustache Sample</title>
</head>
<body>
  <h2>Titles</h2>
  <ul id="talktitles">
    <script id="speakers-template" type="text/template">
  </script>
  </ul>
</body>
</html>

胡子样本
标题

我做错了什么?

您的
输出.json
与模板不一致。模板等待json文件中的
options
值,但没有这样的键

正确的输出json应该如下所示:

{
  options: [{
      "name": "John Doe",
      "age": "30"
    }, {
      "name": "Alex Len",
      "age": "27"
    }, {
     "name": "Debbie John",
     "age": "36"
    }]
}
附加 您还可以使用
因此,您可以将模板部件更改为

    {{#.}}
      <li>{{{name}}}, {{{age}}}</li>
    {{/.}}
{{}
  • {{{name}}},{{{{age}}
  • {{/.}}
    您的
    输出.json
    与模板不一致。模板等待json文件中的
    options
    值,但没有这样的键

    正确的输出json应该如下所示:

    {
      options: [{
          "name": "John Doe",
          "age": "30"
        }, {
          "name": "Alex Len",
          "age": "27"
        }, {
         "name": "Debbie John",
         "age": "36"
        }]
    }
    
    附加 您还可以使用
    因此,您可以将模板部件更改为

        {{#.}}
          <li>{{{name}}}, {{{age}}}</li>
        {{/.}}
    
    {{}
    
  • {{{name}}},{{{{age}}
  • {{/.}}
    如果JSON中没有“选项”键怎么办?那么,我将如何迭代json数据?更新:谢谢你的加入。效果很好。谢谢:)如果JSON中没有“选项”键怎么办?那么,我将如何迭代json数据?更新:谢谢你的加入。效果很好。谢谢:)