使用汇编,使用一个模板文件从多个数据文件生成HTML文件? [英] Using Assemble, generate HTML files from multiple data files using one template file?

查看:115
本文介绍了使用汇编,使用一个模板文件从多个数据文件生成HTML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



更详细地说,我有12个JSON文件,分别表示数据烹饪食谱,以及一个Handlebars文件recipe.hbs,其布局文件为default.hbs。



我想根据每个JSON文件输出一个HTML文件。



文件结构:

  /模板
/数据
recipe1.json
recipe2.json等
/ layouts
default.hbs
/ pages
食谱。 hbs

输出:

  /输出
recipe1.html(基于recipe1.json)
recipe2.html(基于recipe2.json)等

这听起来像应该可以使用集合,但我似乎无法围绕如何设置Grunt文件。



谢谢。

解决方案

我创建了这个要点: https:// gist.github.com/doowb/ca6f3321a05f6ac727e5
拥有 Gruntfile.js 文件中的代码来动态创建使用组合配方模板和配方数据的页面列表。


这些行是创建页面的关键数组。他们可以被移动到另一个文件中,并要求使grunt文件更清洁。



这一行页面数组添加到页面 //gist.github.com/doowb/ca6f3321a05f6ac727e5#file-gruntfile-js-L50rel =nofollow>这一行来设置dest而不需要拉入任何其他页面。



希望这会有所帮助。


I am trying to use Assemble to generate multiple static files from multiple data files using one template.

In more detail, I have 12 JSON files representing the data for cooking recipes, and one Handlebars file, recipe.hbs, with a layout file of default.hbs.

I want to output an HTML file based on each of those JSON files.

File Structure:

/templates
    /data
        recipe1.json
        recipe2.json, etc.
    /layouts
        default.hbs
    /pages
        recipe.hbs

Output:

/output
    recipe1.html (based on recipe1.json)
    recipe2.html (based on recipe2.json), etc.

It sounds like this should be possible using collections, but I can't seem to wrap my head around how to set up the Grunt file.

Thanks.

解决方案

I created this gist: https://gist.github.com/doowb/ca6f3321a05f6ac727e5 that has the code in the Gruntfile.js file for dynamically creating a pages list with the combined recipe template and the recipe data.

These lines are the key to creating the pages array. They could be moved into another file and required in to make the grunt file cleaner.

This line adds the pages array to the pages options on assemble so they'll be built.

If you don't have any other pages to be built, use this line to set the dest without pulling in any other pages.

Hope this helps.

这篇关于使用汇编,使用一个模板文件从多个数据文件生成HTML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆