grunt-ejs-static
Advanced tools
Comparing version 0.3.22 to 0.4.0
{ | ||
"home": { | ||
"meta": { | ||
"title": "Home", | ||
"description": "Compile static html from ejs templates", | ||
"keywords": "gruntplugin, ejs, static html, static site generator, templates, templating engine, template, embedded js" | ||
} | ||
"path_to_data": [ | ||
"demo/dev/data/global.json", | ||
{"demo/dev/data/meta.json": "home"} | ||
] | ||
}, | ||
"about": { | ||
"meta": { | ||
"title": "About", | ||
"description": "Compile static html from ejs templates", | ||
"keywords": "gruntplugin, ejs, static html, static site generator, templates, templating engine, template, embedded js" | ||
} | ||
"path_to_layout": "demo/dev/layouts/default.ejs", | ||
"path_to_data": [ | ||
"demo/dev/data/global.json", | ||
{"demo/dev/data/meta.json": "about"} | ||
] | ||
}, | ||
"work": { | ||
"path_to_layout": "demo/dev/layouts/work.ejs", | ||
"meta": { | ||
"title": "Work", | ||
"description": "Compile static html from ejs templates", | ||
"keywords": "gruntplugin, ejs, static html, static site generator, templates, templating engine, template, embedded js" | ||
} | ||
"path_to_layout": "demo/dev/layouts/default.ejs", | ||
"path_to_data": [ | ||
"demo/dev/data/global.json", | ||
{"demo/dev/data/meta.json": "work"} | ||
] | ||
}, | ||
"contact": { | ||
"meta": { | ||
"title": "Contact", | ||
"description": "Compile static html from ejs templates", | ||
"keywords": "gruntplugin, ejs, static html, static site generator, templates, templating engine, template, embedded js" | ||
} | ||
"path_to_layout": "demo/dev/layouts/default.ejs", | ||
"path_to_data": [ | ||
"demo/dev/data/global.json", | ||
{"demo/dev/data/meta.json": "contact"} | ||
] | ||
}, | ||
@@ -39,7 +37,6 @@ | ||
"path_to_layout": "demo/dev/layouts/experiment.ejs", | ||
"meta": { | ||
"title": "Flexible Box Model", | ||
"description": "Future Proof Flexbox", | ||
"keywords": "" | ||
} | ||
"path_to_data": [ | ||
"demo/dev/data/global.json", | ||
{"demo/dev/data/meta.json": "flexbox"} | ||
] | ||
}, | ||
@@ -50,9 +47,8 @@ | ||
"path_to_layout": "demo/dev/layouts/project.ejs", | ||
"meta": { | ||
"title": "Responsive Design Project", | ||
"description": "Some description here", | ||
"keywords": "" | ||
} | ||
"path_to_data": [ | ||
"demo/dev/data/global.json", | ||
{"demo/dev/data/meta.json": "project_name"} | ||
] | ||
} | ||
} | ||
} |
189
Gruntfile.js
@@ -19,2 +19,3 @@ /* | ||
'tasks/*.js', | ||
'lib/*.js', | ||
'<%= nodeunit.tests %>', | ||
@@ -32,7 +33,8 @@ ], | ||
post_optimize: [ | ||
'demo/production/pages', | ||
'demo/production/pages', | ||
'demo/production/templates', | ||
'demo/production/head.ejs', | ||
'demo/production/scripts.ejs' | ||
] | ||
], | ||
test: ['tests/test*'] | ||
}, | ||
@@ -71,3 +73,3 @@ | ||
'demo/production/scripts.ejs' | ||
] | ||
] | ||
}, | ||
@@ -89,8 +91,7 @@ | ||
path_to_layouts: 'demo/dev/layouts/', | ||
global_data: 'global' | ||
// index_page: 'home', | ||
// parent_dirs: false, | ||
// underscores_to_dashes: true, | ||
// file_extension: '.html', | ||
// path_separator: '/' | ||
index_page: 'home', | ||
parent_dirs: false, | ||
underscores_to_dashes: true, | ||
file_extension: '.html', | ||
underscore: true | ||
} | ||
@@ -106,7 +107,141 @@ }, | ||
underscores_to_dashes: true, | ||
file_extension: '.html' | ||
} | ||
}, | ||
// test that parent_dirs false works as expected | ||
test1: { | ||
options: { | ||
dest: 'tests/test1', | ||
path_to_data: 'demo/dev/data/data.json', | ||
path_to_layouts: 'demo/dev/layouts', | ||
index_page: 'home', | ||
parent_dirs: false, | ||
underscores_to_dashes: true, | ||
file_extension: '.html' | ||
} | ||
}, | ||
// test that parent_dirs true works as expected | ||
test2: { | ||
options: { | ||
dest: 'tests/test2', | ||
path_to_data: 'demo/dev/data/data.json', | ||
path_to_layouts: 'demo/dev/layouts', | ||
index_page: 'home', | ||
parent_dirs: true, | ||
underscores_to_dashes: true, | ||
file_extension: '.html' | ||
} | ||
}, | ||
// test that underscores_to_dashes true works as expected | ||
test3: { | ||
options: { | ||
dest: 'tests/test3', | ||
path_to_data: 'demo/dev/data/data.json', | ||
path_to_layouts: 'demo/dev/layouts', | ||
index_page: 'home', | ||
parent_dirs: false, | ||
underscores_to_dashes: true, | ||
file_extension: '.html' | ||
} | ||
}, | ||
// test that underscores_to_dashes false works as expected | ||
test4: { | ||
options: { | ||
dest: 'tests/test4', | ||
path_to_data: 'demo/dev/data/data.json', | ||
path_to_layouts: 'demo/dev/layouts', | ||
index_page: 'home', | ||
parent_dirs: false, | ||
underscores_to_dashes: false, | ||
file_extension: '.html' | ||
} | ||
}, | ||
// test that changing file_extension works as expected | ||
test5: { | ||
options: { | ||
dest: 'tests/test5', | ||
path_to_data: 'demo/dev/data/data.json', | ||
path_to_layouts: 'demo/dev/layouts', | ||
index_page: 'home', | ||
parent_dirs: false, | ||
underscores_to_dashes: true, | ||
file_extension: '.php' | ||
} | ||
}, | ||
// test include helper functions as an object | ||
test6: { | ||
options: { | ||
dest: 'tests/test6', | ||
path_to_data: 'test/data/helper_function_test.json', | ||
path_to_layouts: 'demo/dev/layouts', | ||
index_page: 'home', | ||
parent_dirs: false, | ||
underscores_to_dashes: true, | ||
file_extension: '.html', | ||
global_data: 'global', | ||
path_separator: '/' | ||
underscore: true, | ||
helpers: { | ||
highlight: function(text) { | ||
return "<h1 style='color:red;'>" + text + "</h1>"; | ||
}, | ||
build_url: function(scheme, hostname, path, queries) { | ||
console.log('this hnappen ' + queries); | ||
var url = ""; | ||
url += scheme; | ||
url += "://"; | ||
url += hostname; | ||
url += path; | ||
if (queries.length > 0) { | ||
url += "?"; | ||
queries.forEach(function(query, index) { | ||
if (index === 0) { | ||
url += query; | ||
} else { | ||
url += "&" + query; | ||
} | ||
}); | ||
} | ||
return url; | ||
}, | ||
print_object: function(obj) { | ||
var output = ''; | ||
for(var property in obj) { | ||
output += property + ': ' + obj[property]+'; \n'; | ||
} | ||
console.log(output); | ||
} | ||
} | ||
} | ||
}, | ||
// test include helper functions as an array | ||
test7: { | ||
options: { | ||
dest: 'tests/test7', | ||
path_to_data: 'test/data/helper_function_test.json', | ||
path_to_layouts: 'demo/dev/layouts', | ||
index_page: 'home', | ||
parent_dirs: false, | ||
underscores_to_dashes: true, | ||
file_extension: '.html', | ||
underscore: true, | ||
helpers: [ | ||
'demo/dev/js/site/helper_functions1.js', | ||
'demo/dev/js/site/helper_functions2.js' | ||
] | ||
} | ||
}, | ||
// test include helper functions as an string | ||
test8: { | ||
options: { | ||
dest: 'tests/test8', | ||
path_to_data: 'test/data/helper_function_test.json', | ||
path_to_layouts: 'demo/dev/layouts', | ||
index_page: 'home', | ||
parent_dirs: false, | ||
underscores_to_dashes: true, | ||
file_extension: '.html', | ||
underscore: true, | ||
helpers: 'demo/dev/js/site/helper_functions3.js' | ||
} | ||
} | ||
}, | ||
@@ -116,3 +251,3 @@ | ||
nodeunit: { | ||
tests: ['test/*_test.js'], | ||
tests: ['test/ejs_static_test.js'], | ||
}, | ||
@@ -138,6 +273,4 @@ | ||
'clean:preview', | ||
'copy:preview', | ||
'copy:preview', | ||
'ejs_static:preview' | ||
// , | ||
// 'nodeunit' | ||
]); | ||
@@ -149,13 +282,25 @@ | ||
'copy:optimize', | ||
'useminPrepare', | ||
'concat', | ||
'cssmin', | ||
'uglify', | ||
'usemin', | ||
'useminPrepare', | ||
'concat', | ||
'cssmin', | ||
'uglify', | ||
'usemin', | ||
'ejs_static:optimize', | ||
'clean:post_optimize' | ||
// , | ||
// 'nodeunit' | ||
]); | ||
// test | ||
grunt.registerTask('test', [ | ||
'clean:test', | ||
'ejs_static:test1', | ||
'ejs_static:test2', | ||
'ejs_static:test3', | ||
'ejs_static:test4', | ||
'ejs_static:test5', | ||
'ejs_static:test6', | ||
'ejs_static:test7', | ||
'ejs_static:test8', | ||
'nodeunit' | ||
]); | ||
}; |
@@ -10,23 +10,4 @@ | ||
grunt.log.debug('get_data(): executed and the current key is ' + key); | ||
// get the data for this file | ||
var this_file_data = files[key]; | ||
// add any additional data specified in pages declaration json file | ||
if (typeof this_file_data.path_to_data !== 'undefined') { | ||
grunt.log.debug('get_data(): the path to data is ' + this_file_data.path_to_data); | ||
data_type_router(this_file_data, this_file_data.path_to_data); | ||
} else { | ||
grunt.log.debug('get_data(): the type of data is undefined'); | ||
} | ||
grunt.log.debug('the file data for ' + key + ' is ' + JSON.stringify(this_file_data, null, 2)); | ||
return this_file_data; | ||
// PRIVATE FUNCTIONS | ||
// | ||
// | ||
@@ -36,3 +17,3 @@ // route data based on data type (string, object, or array) | ||
// if path_to_data is a string | ||
// if path_to_data is a string | ||
if (typeof additional_data === 'string') { | ||
@@ -119,8 +100,33 @@ | ||
// | ||
// END PRIVATE FUNCTIONS | ||
// | ||
// END PRIVATE FUNCTIONS | ||
grunt.log.debug('get_data(): executed and the current key is ' + key); | ||
// get the data for this file | ||
var this_file_data = files[key]; | ||
// add the current file name to the additional data | ||
this_file_data.file_name = key; | ||
// create a version with any underscores converted to dashes in the file name | ||
this_file_data.dashed_file_name = this_file_data.file_name.replace(/_/g, "-"); | ||
// add any additional data specified in pages declaration json file | ||
if (typeof this_file_data.path_to_data !== 'undefined') { | ||
grunt.log.debug('get_data(): the path to data is ' + this_file_data.path_to_data); | ||
data_type_router(this_file_data, this_file_data.path_to_data); | ||
} else { | ||
grunt.log.debug('get_data(): the type of data is undefined'); | ||
} | ||
grunt.log.debug('the file data for ' + key + ' is ' + JSON.stringify(this_file_data, null, 2)); | ||
return this_file_data; | ||
} | ||
// end get the data | ||
// end get the data | ||
}; | ||
}; |
@@ -5,3 +5,3 @@ | ||
// private functions | ||
// | ||
// | ||
var get_files = require('./get_files').get_files, | ||
@@ -11,9 +11,10 @@ get_data = require('./data').get_data, | ||
render_file = require('./render').render_file, | ||
write_file = require('./write_file').write_file; | ||
write_file = require('./write_file').write_file, | ||
get_helper_functions = require('./get_helper_functions').get_helper_functions; | ||
// | ||
// | ||
// end private functions | ||
// public functions | ||
// | ||
// | ||
return { | ||
@@ -34,6 +35,9 @@ | ||
// write file | ||
write_file: write_file | ||
} | ||
// | ||
write_file: write_file, | ||
// get helper functions | ||
get_helper_functions: get_helper_functions | ||
}; | ||
// | ||
// end public functions | ||
@@ -43,2 +47,2 @@ | ||
module.exports = exports = new EJS_Static(); | ||
module.exports = exports = new EJS_Static(); |
@@ -17,7 +17,7 @@ | ||
grunt.log.debug('Data successfully imported'); | ||
// return the data | ||
return files_data; | ||
grunt.log.debug('Data successfully imported'); | ||
} else { | ||
@@ -29,6 +29,6 @@ | ||
} | ||
// end get data specified in ejs_static options | ||
// end get data specified in ejs_static options | ||
} | ||
} | ||
}; | ||
}; |
@@ -10,19 +10,2 @@ | ||
grunt.log.debug('get_layout(): executed and the key is ' + key); | ||
// get path to layout | ||
var path_to_layout = path_to_layout_generator(key, files, options); | ||
// get the layout | ||
var layout = grunt.file.read(path_to_layout); | ||
// create layout data object to return both path_to_layout and layout | ||
var layout_data = { | ||
path_to_layout: path_to_layout, | ||
layout: layout | ||
} | ||
grunt.log.debug('The layout data is ' + JSON.stringify(layout_data, null, 2)); | ||
return layout_data; | ||
// PRIVATE FUNCTIONS | ||
@@ -34,3 +17,3 @@ // | ||
// if path_to_layout exists | ||
// if path_to_layout is specified in the controller json file | ||
if (typeof files[key].path_to_layout === 'string') { | ||
@@ -40,3 +23,26 @@ | ||
// if a layouts dir is specified | ||
// if a layouts dir and layout file are both specified | ||
// layouts dir is specified in Gruntfile in ejs_static options | ||
// layout file is specified in the controller json file | ||
} else if ((typeof options.path_to_layouts === 'string') && (typeof files[key].layout === 'string')) { | ||
grunt.log.debug('Layouts dir was specified in Gruntfile and layout was specified in controller json file'); | ||
// if files[key].layout includes .ejs file extension, use it without appending .ejs | ||
if ( files[key].layout.indexOf('.ejs') > 0 ) { | ||
// build path to layout by concatenating layout dir path with layout file | ||
return path.join(options.path_to_layouts, files[key].layout); | ||
// otherwise append .ejs file extension | ||
} else { | ||
// build path to layout by concatenating layout dir path with layout file | ||
return path.join(options.path_to_layouts, files[key].layout + '.ejs'); | ||
} | ||
// if a layouts dir is specified but layout file is not | ||
// this seems kind of brittle, so may change to throwing error if neither path_to_layout nor layout is specified | ||
} else if (typeof options.path_to_layouts === 'string') { | ||
@@ -47,7 +53,7 @@ | ||
} else { | ||
grunt.fail.warn('A value for path_to_layout is required, please specify in data file for key: ' + key); | ||
} | ||
grunt.fail.warn('path_to_layout_generator(): path was not generated for key: ' + key); | ||
} | ||
} | ||
// end generate path to layout | ||
// end generate path to layout | ||
@@ -66,4 +72,7 @@ // search the layouts dir for a dir or file that matches key | ||
var path_to_layout = grunt.file.expand(search_pattern1, search_pattern2, search_pattern3); | ||
var search_pattern4 = path.join(options.path_to_layouts, '**', key, 'index.html'); | ||
grunt.log.debug('search pattern4 is ' + search_pattern4); | ||
var path_to_layout = grunt.file.expand(search_pattern1, search_pattern2, search_pattern3, search_pattern4); | ||
// if exactly one path to layout is returned, return that path | ||
@@ -84,4 +93,4 @@ if (path_to_layout.length === 1) { | ||
grunt.fail.warn('More than 1 path to layout file was not found for key: ' + key + | ||
'\nThe paths found were: \n' + multiple_layout_paths + | ||
grunt.fail.warn('More than 1 path to layout file was not found for key: ' + key + | ||
'\nThe paths found were: \n' + multiple_layout_paths + | ||
'\nResolve in the project dir structure and/or data file\n'); | ||
@@ -97,8 +106,25 @@ | ||
// | ||
// | ||
// END PRIVATE FUNCTIONS | ||
grunt.log.debug('get_layout(): executed and the key is ' + key); | ||
// get path to layout | ||
var path_to_layout = path_to_layout_generator(key, files, options); | ||
// get the layout | ||
var layout = grunt.file.read(path_to_layout); | ||
// create layout data object to return both path_to_layout and layout | ||
var layout_data = { | ||
path_to_layout: path_to_layout, | ||
layout: layout | ||
}; | ||
grunt.log.debug('The layout data is ' + JSON.stringify(layout_data, null, 2)); | ||
return layout_data; | ||
} | ||
// end prepare layout file | ||
}; | ||
}; |
var grunt = require('grunt'); | ||
var ejs = require('ejs'); | ||
var _ = require('underscore'); | ||
@@ -8,3 +9,3 @@ module.exports = exports = { | ||
// render the file | ||
render_file: function(layout_data, file_data) { | ||
render_file: function(layout_data, file_data, helpers) { | ||
@@ -18,7 +19,16 @@ grunt.log.debug('render_file(): executed'); | ||
file_data.filename = layout_data.path_to_layout; | ||
grunt.log.debug('THIS DATA FILENAME IS ' + file_data.filename); | ||
// add any helpers to the file_data object | ||
if (_.keys(helpers).length > 0) { | ||
_.extend(file_data, helpers); | ||
grunt.log.debug('render.js render_file() the value of helpers is :' + Object.keys(helpers)); | ||
} | ||
grunt.log.debug('data available for this template ' + Object.keys(file_data)); | ||
// render the template as html and return the result | ||
var rendered_file = ejs.render(layout_data.layout, file_data); | ||
grunt.log.debug(rendered_file.toString()); | ||
return rendered_file; | ||
@@ -25,0 +35,0 @@ |
@@ -9,4 +9,2 @@ | ||
create_file_router(key, files, rendered_file, options); | ||
// PRIVATE FUNCTIONS | ||
@@ -46,5 +44,6 @@ // | ||
// create path to the rendered html file | ||
var path_to_file = path.join(options.dest, 'index' + options.file_extension), | ||
path_to_file = underscores_to_dashes(path_to_file); | ||
var path_to_file = path.join(options.dest, 'index' + options.file_extension); | ||
path_to_file = underscores_to_dashes(path_to_file); | ||
grunt.log.debug('path to file is ' + path_to_file); | ||
@@ -64,2 +63,4 @@ | ||
var path_to_file = ''; | ||
// if the parent_dirs option is true | ||
@@ -69,21 +70,27 @@ if (options.parent_dirs) { | ||
// create the file as an index.html file inside a parent directory | ||
var path_to_file = path.join(options.dest, files[key].category, key, 'index' + options.file_extension), | ||
path_to_file = underscores_to_dashes(path_to_file); | ||
path_to_file = path.join(options.dest, files[key].category, key, 'index' + options.file_extension); | ||
grunt.log.debug('path to html file is ' + path_to_file); | ||
path_to_file = underscores_to_dashes(path_to_file); | ||
grunt.log.debug('path to html file is ' + path_to_file); | ||
// write file to the destination directory | ||
grunt.file.write(path_to_file, rendered_file); | ||
grunt.log.debug('Rendered category file written to ' + path_to_file); | ||
} else { | ||
// create the file with the key as the file name | ||
var path_to_file = path.join(options.dest, files[key].category, key + options.file_extension), | ||
path_to_file = underscores_to_dashes(path_to_file); | ||
path_to_file = path.join(options.dest, files[key].category, key + options.file_extension); | ||
path_to_file = underscores_to_dashes(path_to_file); | ||
grunt.log.debug('path to html file is ' + path_to_file); | ||
// write file to the destination directory | ||
grunt.file.write(path_to_file, rendered_file); | ||
grunt.log.debug('Rendered category file written to ' + path_to_file); | ||
} | ||
// write file to the destination directory | ||
grunt.file.write(path_to_file, rendered_file); | ||
grunt.log.debug('Rendered category file written to ' + path_to_file); | ||
} | ||
@@ -97,2 +104,4 @@ // end create a file that is in a category | ||
var path_to_file = ''; | ||
// if the parent_dirs option is true | ||
@@ -102,21 +111,27 @@ if (options.parent_dirs) { | ||
// create path to the rendered html file | ||
var path_to_file = path.join(options.dest, key, 'index' + options.file_extension), | ||
path_to_file = underscores_to_dashes(path_to_file); | ||
path_to_file = path.join(options.dest, key, 'index' + options.file_extension); | ||
grunt.log.debug('path to html file is ' + path_to_file); | ||
path_to_file = underscores_to_dashes(path_to_file); | ||
grunt.log.debug('path to html file is ' + path_to_file); | ||
// write file to the destination directory | ||
grunt.file.write(path_to_file, rendered_file); | ||
grunt.log.debug('Rendered root level file written to ' + path_to_file); | ||
} else { | ||
// create the file with the key as the file name | ||
var path_to_file = path.join(options.dest, key + options.file_extension), | ||
path_to_file = underscores_to_dashes(path_to_file); | ||
path_to_file = path.join(options.dest, key + options.file_extension); | ||
grunt.log.debug('path to html file is ' + path_to_file); | ||
path_to_file = underscores_to_dashes(path_to_file); | ||
grunt.log.debug('path to html file is ' + path_to_file); | ||
// write file to the destination directory | ||
grunt.file.write(path_to_file, rendered_file); | ||
grunt.log.debug('Rendered root level file written to ' + path_to_file); | ||
} | ||
// write file to the destination directory | ||
grunt.file.write(path_to_file, rendered_file); | ||
grunt.log.debug('Rendered root level file written to ' + path_to_file); | ||
} | ||
@@ -131,5 +146,7 @@ // end create a file at root level | ||
var item = item.replace(/_/g, "-"); | ||
var modified_item = ''; | ||
modified_item = item.replace(/_/g, "-"); | ||
grunt.log.debug('underscores_to_dashes was set to true and processed ' + item); | ||
return item; | ||
return modified_item; | ||
@@ -141,6 +158,11 @@ } else { | ||
} | ||
// end make file name url ready by replacing underscores with dashes | ||
// end make file name url ready by replacing underscores with dashes | ||
// | ||
// END PRIVATE FUNCTIONS | ||
create_file_router(key, files, rendered_file, options); | ||
} | ||
}; |
{ | ||
"name": "grunt-ejs-static", | ||
"description": "Compile static html from ejs templates", | ||
"version": "0.3.22", | ||
"version": "0.4.0", | ||
"homepage": "https://github.com/shaekuronen/grunt-ejs-static", | ||
@@ -31,6 +31,7 @@ "author": { | ||
"dependencies": { | ||
"ejs": "~0.8.4" | ||
"ejs": "~0.8.4", | ||
"underscore": "~1.6.0" | ||
}, | ||
"devDependencies": { | ||
"grunt-contrib-jshint": "~0.1.1", | ||
"grunt-contrib-jshint": "~0.6.2", | ||
"grunt-contrib-clean": "~0.4.0", | ||
@@ -37,0 +38,0 @@ "grunt-contrib-nodeunit": "~0.1.2", |
161
README.md
# grunt-ejs-static | ||
Grunt EJS Static takes an MVC approach to building a static site generator using EJS and Grunt. | ||
[![Build Status](https://travis-ci.org/shaekuronen/grunt-ejs-static.png?branch=master)](https://travis-ci.org/shaekuronen/grunt-ejs-static) | ||
Models can be defined in multiple JSON files, which helps keep logic out of the templates and the data more maintainable. | ||
Grunt EJS Static takes an MVC approach to building a static site generator using EJS and Grunt. | ||
Views are defined in layout files, which are top-level templates utilizing EJS Includes to manage layout. | ||
Models can be defined in multiple JSON files, which helps keep logic out of the templates and the data more maintainable. | ||
The controller is a JSON file (such as routes.json) which defines files to render, as well as each file's layout and any additional data needed for rendering. | ||
Views are defined in layout files, which are top-level templates utilizing EJS Includes to manage layout. | ||
For examples, please see demo/ and Usage section below | ||
The controller is a JSON file (such as routes.json) which defines files to render, as well as each file's layout and any additional data needed for rendering. | ||
For examples, please see demo/ and Usage section below. | ||
There is also a project boilerplate available [grunt-ejs-static-boilerplate](https://github.com/shaekuronen/grunt-ejs-static-boilerplate) | ||
## Getting Started | ||
@@ -35,8 +39,10 @@ This plugin requires Grunt `~0.4.1` | ||
Conditionally required options are path_to_layouts. | ||
Conditionally required options are path_to_layouts. If options.path_to_layouts is specified, layout can be specified in the controller json (such as routes.json). | ||
If layout paths are specified in data.json (recommended), options.path_to_layouts is not necessary. | ||
If path_to_layout is specified in routes.json, options.path_to_layouts is not necessary. This option does not work well with grunt-usemin, so if you're using it may be better to specify options.path_to_layouts + layout in controller json file. | ||
If layout paths are not specified, ejs_static falls back to searching the dir specified in options.path_to_layouts. | ||
Important note: to use a layout for multiple pages, there are two methods. One is to specify path_to_layout in routes.json. The other is to specify options.path_to_layouts + layout in controller json file. | ||
If layout paths are not specified, ejs_static falls back to searching the dir specified in options.path_to_layouts. | ||
```js | ||
@@ -103,3 +109,3 @@ grunt.initConfig({ | ||
``` | ||
Generates this JSON data model | ||
Generates this JSON data model | ||
```json | ||
@@ -212,3 +218,3 @@ { | ||
<div class="container"> | ||
<footer> | ||
@@ -230,3 +236,3 @@ <p>© Company 2012</p> | ||
#### options.dest | ||
#### options.dest | ||
Required | ||
@@ -247,3 +253,3 @@ Type: `String` | ||
This is the path to the layout files directory | ||
This is the path to the layout files directory | ||
@@ -254,3 +260,3 @@ #### options.index_page | ||
This sets the index page for the site | ||
This sets the index page for the site | ||
@@ -281,7 +287,114 @@ #### options.parent_dirs | ||
This defines the file extension of rendered files | ||
This defines the file extension of rendered files | ||
Defaults to .html, but could be .php, .aspx, etc | ||
Defaults to .html, but could be .php, .aspx, etc | ||
#### options.helpers | ||
Optional | ||
Type: `String`, `Object`, or `Array` | ||
Helper functions can be defined in 3 ways. If a string, indicates the path to a single file containing one or more helper functions. An array indicates multiple paths to files containing one or more helper functions. An object defines one or more helper functions inline (in the Gruntfile). The key is the function name and the value is the function expression. | ||
```javascript | ||
ejs_static: { | ||
// single file example | ||
string: { | ||
options: { | ||
dest: 'dist', | ||
path_to_data: 'path/to/data.json', | ||
path_to_layouts: 'path/to/layouts', | ||
underscore: true, | ||
helpers: 'demo/dev/js/site/helper_functions3.js' | ||
} | ||
}, | ||
// inline example | ||
object: { | ||
options: { | ||
dest: 'dist', | ||
path_to_data: 'path/to/data.json', | ||
path_to_layouts: 'path/to/layouts', | ||
underscore: true, | ||
helpers: { | ||
highlight: function(text) { | ||
return "<h1 style='color:red;'>" + text + "</h1>"; | ||
}, | ||
print_object: function(obj) { | ||
var output = ''; | ||
for(var property in obj) { | ||
output += property + ': ' + obj[property]+'; \n'; | ||
} | ||
console.log(output); | ||
} | ||
} | ||
} | ||
}, | ||
// multiple files example | ||
array: { | ||
options: { | ||
dest: 'dist', | ||
path_to_data: 'path/to/data.json', | ||
path_to_layouts: 'path/to/layouts', | ||
underscore: true, | ||
helpers: [ | ||
'demo/dev/js/site/helper_functions1.js', | ||
'demo/dev/js/site/helper_functions2.js' | ||
] | ||
} | ||
} | ||
} | ||
``` | ||
If included as a file, needs to be wrapped w/ module.exports | ||
```javascript | ||
module.exports = exports = { | ||
build_url: function(scheme, hostname, path, queries) { | ||
console.log('this hnappen ' + queries); | ||
var url = ""; | ||
url += scheme; | ||
url += "://"; | ||
url += hostname; | ||
url += path; | ||
if (queries.length > 0) { | ||
url += "?"; | ||
queries.forEach(function(query, index) { | ||
if (index === 0) { | ||
url += query; | ||
} else { | ||
url += "&" + query; | ||
} | ||
}); | ||
} | ||
return url; | ||
}, | ||
print_object: function(obj) { | ||
var output = ''; | ||
for(var property in obj) { | ||
output += property + ': ' + obj[property]+'; \n'; | ||
} | ||
console.log(output); | ||
}, | ||
highlight: function(text) { | ||
return "<h1 style='color:red;'>" + text + "</h1>"; | ||
} | ||
}; | ||
``` | ||
which can then be used in a template like this | ||
```ejs | ||
<%- highlight('This Text') -%> | ||
<%= build_url("https", "www.google.com", "/search", ['q=unicorns', 'oq=unicorns']) %> | ||
<% print_object(this); %> | ||
``` | ||
#### options.underscore | ||
Optional | ||
Type: `Boolean` | ||
Default: False | ||
Defines whether Underscore is available in the templates at render time. | ||
### Examples | ||
@@ -307,3 +420,3 @@ | ||
This builds static html into demo/production/ | ||
This builds static html into demo/production/ | ||
@@ -314,7 +427,19 @@ ## Contributing | ||
## Release History | ||
### 0.3.8 | ||
* Added support for helper functions. | ||
* Added option to make Underscore JS available in the templates. | ||
### 0.3.7 | ||
* Added dashed_file_name variable to make easier to include page name into CSS. | ||
* Added option to specify layout (vs. path_to_layout) in controller json file. This can be used in combination with path_to_layouts, which is specified in Gruntfile ejs_static options. | ||
### 0.3.5 | ||
* Added file_name variable to allow template extension. | ||
* See discussion on [Github](https://github.com/shaekuronen/grunt-ejs-static/issues/2). | ||
### 0.3.2 | ||
* Moved logic into modules. | ||
* Improved management of data files. Combine files, or parse for a specific key, prior to rendering. | ||
* Improved management of data files. Combine files, or parse for a specific key, prior to rendering. | ||
### 0.3.0 | ||
### 0.3.0 | ||
* Complete rewrite. More flexible, less opinionated. | ||
@@ -321,0 +446,0 @@ * Now data-driven. Files to be rendered are defined in JSON file, not in the file structure. |
@@ -19,2 +19,5 @@ /* | ||
// get underscore | ||
var _ = require('underscore'); | ||
// set defaults for options | ||
@@ -25,5 +28,31 @@ var options = this.options({ | ||
file_extension: '.html', | ||
underscores_to_dashes: true | ||
underscores_to_dashes: true, | ||
underscore: false | ||
}); | ||
// helper functions | ||
// create a global object to attach any helper functions to | ||
var helpers_global = {}; | ||
if (options.helpers) { | ||
// process specified helper functions | ||
var helpers = ejs_static.get_helper_functions(options); | ||
// add them to the global object | ||
_.extend(helpers_global, helpers); | ||
grunt.log.debug('helpers are ' + helpers); | ||
} else { | ||
grunt.log.debug('no helper functions in options object'); | ||
} | ||
// make underscore available in the templates | ||
if (options.underscore === true) { | ||
_.extend(helpers_global, _); | ||
} else { | ||
grunt.log.debug('options.underscore not set to Boolean true'); | ||
} | ||
// end helper functions | ||
// get the files to render, which are declared in the options.path_to_data JSON file | ||
@@ -41,4 +70,4 @@ var files = ejs_static.get_files(options); | ||
// render the file | ||
var rendered_file = ejs_static.render_file(layout_data, file_data); | ||
// render the file | ||
var rendered_file = ejs_static.render_file(layout_data, file_data, helpers_global); | ||
@@ -45,0 +74,0 @@ // write the file to the destination directory |
'use strict'; | ||
var grunt = require('grunt'); | ||
var path = require('path'); | ||
@@ -26,24 +27,77 @@ /* | ||
exports.ejs_static = { | ||
setUp: function(done) { | ||
// setup here if necessary | ||
done(); | ||
test1: function(test) { | ||
test.expect(1); | ||
var actual = grunt.file.read('tests/test1/index.html'); | ||
var expected = grunt.file.read('test/expected/index.html'); | ||
test.equal(actual, expected, 'Test1 should output files without parent dirs'); | ||
test.done(); | ||
}, | ||
default_options: function(test) { | ||
test2: function(test) { | ||
test.expect(1); | ||
var actual = grunt.file.read('tmp/default_options'); | ||
var expected = grunt.file.read('test/expected/default_options'); | ||
test.equal(actual, expected, 'should describe what the default behavior is.'); | ||
var actual = grunt.file.read('tests/test2/about/index.html'); | ||
var expected = grunt.file.read('test/expected/about/index.html'); | ||
test.equal(actual, expected, 'Test2 should output files with parent dirs'); | ||
test.done(); | ||
}, | ||
custom_options: function(test) { | ||
test3: function(test) { | ||
test.expect(1); | ||
var actual = grunt.file.read('tmp/custom_options'); | ||
var expected = grunt.file.read('test/expected/custom_options'); | ||
test.equal(actual, expected, 'should describe what the custom option(s) behavior is.'); | ||
var file = grunt.file.read('tests/test3/project/project-name.html'); | ||
test.ok(file, 'Test3 should find file project/project-name.html'); | ||
test.done(); | ||
}, | ||
test4: function(test) { | ||
test.expect(1); | ||
var file = grunt.file.read('tests/test4/project/project_name.html'); | ||
test.ok(file, 'Test4 should find file project/project_name.html'); | ||
test.done(); | ||
}, | ||
test5: function(test) { | ||
test.expect(1); | ||
var file = grunt.file.read('tests/test5/index.php'); | ||
test.ok(file, 'Test5 should find file index.php'); | ||
test.done(); | ||
}, | ||
test6: function(test) { | ||
test.expect(1); | ||
var file = grunt.file.read('tests/test6/helper-functions.html'); | ||
test.ok(file, 'Test6 should find file helper-functions.html'); | ||
test.done(); | ||
}, | ||
test7: function(test) { | ||
test.expect(1); | ||
var file = grunt.file.read('tests/test7/helper-functions.html'); | ||
test.ok(file, 'Test7 should find file helper-functions.html'); | ||
test.done(); | ||
}, | ||
test8: function(test) { | ||
test.expect(1); | ||
var file = grunt.file.read('tests/test8/helper-functions.html'); | ||
test.ok(file, 'Test8 should find file helper-functions.html'); | ||
test.done(); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
2585431
125
50840
444
3
1
+ Addedunderscore@~1.6.0
+ Addedunderscore@1.6.0(transitive)