Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

gruntjs - Using Assemble, generate HTML files from multiple data files using one template file?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...