NPM mustache-render使用单个模板和多个JSON创建多个HTML

时间:2017-09-07 16:05:05

标签: javascript json node.js gruntjs mustache

我使用npm grunt-mustache-render(https://www.npmjs.com/package/grunt-mustache-render)来生成HTML文件。我可以使用1个json和1个模板创建html。现在,我正在尝试的是基于1个模板和多个json文件创建多个HTML文件。我不想一个一个地明确命名所有的jsons。相反,我想指定所有json文件的路径,模板的路径,然后指定dist路径,以便它根据json文件名生成HTML文件。

这是我的笨拙任务:

mustache_render: {
      json_data: {
        files: [
          {expand: true,
           src: 'content/*.json',
           template: 'templates/menu-content.mustache',
           dest: 'publish/'}
        ]
      }
    }

但它没有创建HTML文件。相反,它会创建多个带有HTML代码的.json文件。另请注意,它会在publish / content /文件夹中创建文件,而不是发布/我打算将它们放在哪里。输出如下所示:

Output publish/content/page-one.json:
>> 2-key object into templates/menu-content.mustache from content/page-one.json
Output publish/content/page-three.json:
>> 5-key object into templates/menu-content.mustache from content/page-three.json
Output publish/content/page-two.json:
>> 5-key object into templates/menu-content.mustache from content/page-two.json

我做错了什么?是否有可能用grunt-mustache-render做我正在尝试的事情?

TIA。

1 个答案:

答案 0 :(得分:0)

这可以通过根据var z : Int = 100 z = Int(Double(z) - 0.65 * 19) print(z) // 87 文件动态生成mustache_render任务的配置来实现。

尝试按如下方式配置.json

<强> Gruntfile.js

Gruntfile.js