Html 使用json作为Assembly.io的数据源
是否可以使用Assemble.io中的模板将json文件编译为html。如果可能的话,我如何设置我的GrunFile来实现这一点 我的文件夹结构:Html 使用json作为Assembly.io的数据源,html,json,assemble,Html,Json,Assemble,是否可以使用Assemble.io中的模板将json文件编译为html。如果可能的话,我如何设置我的GrunFile来实现这一点 我的文件夹结构: 资料 productlist1.json productlist2.json productlist3.json productdetail1.json productdetail2.json 模板 product-listing.hbs 产品详细信息.hbs 布局 main.hbs 分部 HTML结果 我想生成以下html文件
- 资料
- productlist1.json
- productlist2.json
- productlist3.json
- productdetail1.json
- productdetail2.json
- 模板
- product-listing.hbs
- 产品详细信息.hbs
- 布局
- main.hbs
- 分部
- productlist1.html
- productlist2.html
- productlist3.html
- productdetail1.html
- productdetail2.html
多亏了你,我才找到了解决办法 稍微修改示例以加载动态夯实:
'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'
]);
};
“严格使用”;
var=要求('lodash');
var path=require('path');
module.exports=函数(grunt){
//展开数据文件并在每个文件路径上循环
var pages=..flant(..map(grunt.file.expand('./src/data/*.json'),函数(filepath){
//读入数据文件
var data=grunt.file.readJSON(文件路径),
fileTemplate=grunt.file.read(“./src/templates/”+data.template);
//创建要添加到“页面”集合的“页面”对象
返回{
//文件名将决定以后如何命名页面
文件名:path.basename(文件路径,path.extname(文件路径)),
//来自json文件的数据
数据:数据,
//添加配方模板作为页面内容
内容:文件模板
};
}));
//项目配置。
grunt.initConfig({
配置:{
src:'src',
dist:“dist”
},
汇编:{
页码:{
选项:{
扁平化:是的,
资产:'/assets',
布局:'/templates/layouts/default.hbs',
分部:'/templates/partials/***.hbs',
//将上面的pages数组添加到Assembly选项上的pages集合中
页数:页数
},
档案:[
//目前,我们需要欺骗grunt和Assembly,将pages文件放入正确的
//使用此模式放置
{dest:'./dist/',src:'!*'}
]
}
}
});
grunt.loadNpmTasks(“汇编”);
grunt.registerTask('default'[
“集合”
]);
};
'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'
]);
};