【问题标题】:Use json as datasource for Assemble.io使用 json 作为 Assemble.io 的数据源
【发布时间】:2014-09-09 07:21:35
【问题描述】:

是否可以使用 Assemble.io 中的模板将 json 文件编译为 html。如果可能的话,我该如何设置我的 gruntfile 呢?

我的文件夹结构:

  • 数据
    • productlist1.json
    • productlist2.json
    • productlist3.json
    • productdetail1.json
    • productdetail2.json
  • 模板
    • product-listing.hbs
    • product-detail.hbs
  • 布局
    • main.hbs
  • 部分
    • ...

HTML 结果

我要生成以下html文件

  • productlist1.html
  • productlist2.html
  • productlist3.html
  • productdetail1.html
  • productdetail2.html

productlist1.json 文件示例

    {
      "template": "product-listing.json",
      "products": [
        {
          "name": "product1",
          "price": "€ 2,40"
        },
        {
          "name": "product2",
          "price: "€ 1,40"
        }
      ]
    }

【问题讨论】:

    标签: html json assemble


    【解决方案1】:

    感谢Using Assemble, generate HTML files from multiple data files using one template file?,刚刚找到了解决方案。

    稍微修改示例以加载动态模板:

    'use strict';
    var _ = require('lodash');
    var path = require('path');
    
    module.exports = function(grunt) {
    
        // expand the data files and loop over each filepath
        var pages = _.flatten(_.map(grunt.file.expand('./src/data/*.json'), function(filepath) {
    
            // read in the data file
            var data = grunt.file.readJSON(filepath),
                fileTemplate = grunt.file.read("./src/templates/" + data.template);
    
            // create a 'page' object to add to the 'pages' collection
            return {
                // the filename will determine how the page is named later
                filename: path.basename(filepath, path.extname(filepath)),
                // the data from the json file
                data: data,
                // add the recipe template as the page content
                content: fileTemplate
            };
        }));
    
        // Project configuration.
        grunt.initConfig({
    
            config: {
                src: 'src',
                dist: 'dist'
            },
    
            assemble: {
                pages: {
                    options: {
                        flatten: true,
                        assets: '<%= config.dist %>/assets',
                        layout: '<%= config.src %>/templates/layouts/default.hbs',
                        partials: '<%= config.src %>/templates/partials/**/*.hbs',
    
                        // add the pages array from above to the pages collection on the assemble options
                        pages: pages
                    },
                    files: [
                        // currently we need to trick grunt and assemble into putting the pages file into the correct
                        // place using this pattern
                        { dest: './dist/', src: '!*' }
                    ]
                }
            }
        });
    
        grunt.loadNpmTasks('assemble');
    
        grunt.registerTask('default', [
            'assemble'
        ]);
    
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 2017-02-01
      • 1970-01-01
      相关资源
      最近更新 更多