grunt-ejs-static
Advanced tools
Comparing version 0.3.7 to 0.3.8
{ | ||
"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"} | ||
] | ||
}, | ||
@@ -13,7 +12,6 @@ | ||
"path_to_layout": "demo/dev/layouts/default.ejs", | ||
"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_data": [ | ||
"demo/dev/data/global.json", | ||
{"demo/dev/data/meta.json": "about"} | ||
] | ||
}, | ||
@@ -23,7 +21,6 @@ | ||
"path_to_layout": "demo/dev/layouts/default.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_data": [ | ||
"demo/dev/data/global.json", | ||
{"demo/dev/data/meta.json": "work"} | ||
] | ||
}, | ||
@@ -33,7 +30,6 @@ | ||
"path_to_layout": "demo/dev/layouts/default.ejs", | ||
"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_data": [ | ||
"demo/dev/data/global.json", | ||
{"demo/dev/data/meta.json": "contact"} | ||
] | ||
}, | ||
@@ -44,7 +40,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"} | ||
] | ||
}, | ||
@@ -55,9 +50,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"} | ||
] | ||
} | ||
} |
@@ -91,3 +91,4 @@ /* | ||
underscores_to_dashes: true, | ||
file_extension: '.html' | ||
file_extension: '.html', | ||
underscore: true | ||
} | ||
@@ -106,2 +107,3 @@ }, | ||
}, | ||
// test that parent_dirs false works as expected | ||
test1: { | ||
@@ -118,2 +120,3 @@ options: { | ||
}, | ||
// test that parent_dirs true works as expected | ||
test2: { | ||
@@ -130,2 +133,3 @@ options: { | ||
}, | ||
// test that underscores_to_dashes true works as expected | ||
test3: { | ||
@@ -142,2 +146,3 @@ options: { | ||
}, | ||
// test that underscores_to_dashes false works as expected | ||
test4: { | ||
@@ -154,2 +159,3 @@ options: { | ||
}, | ||
// test that changing file_extension works as expected | ||
test5: { | ||
@@ -165,3 +171,79 @@ options: { | ||
} | ||
}, | ||
// 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', | ||
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' | ||
} | ||
} | ||
}, | ||
@@ -217,2 +299,5 @@ | ||
'ejs_static:test5', | ||
'ejs_static:test6', | ||
'ejs_static:test7', | ||
'ejs_static:test8', | ||
'nodeunit' | ||
@@ -219,0 +304,0 @@ ]); |
@@ -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(); |
@@ -21,3 +21,3 @@ | ||
return files_data; | ||
} else { | ||
@@ -29,6 +29,6 @@ | ||
} | ||
// end get data specified in ejs_static options | ||
// end get data specified in ejs_static options | ||
} | ||
} | ||
}; | ||
}; |
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 @@ |
{ | ||
"name": "grunt-ejs-static", | ||
"description": "Compile static html from ejs templates", | ||
"version": "0.3.7", | ||
"version": "0.3.8", | ||
"homepage": "https://github.com/shaekuronen/grunt-ejs-static", | ||
@@ -43,3 +43,4 @@ "author": { | ||
"grunt-contrib-cssmin": "~0.6.0", | ||
"grunt-contrib-concat": "~0.3.0" | ||
"grunt-contrib-concat": "~0.3.0", | ||
"underscore": "~1.6.0" | ||
}, | ||
@@ -58,6 +59,3 @@ "peerDependencies": { | ||
"embedded js" | ||
], | ||
"scripts": { | ||
"test": "grunt test" | ||
} | ||
] | ||
} |
@@ -285,3 +285,83 @@ # grunt-ejs-static | ||
#### 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. | ||
```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>"; | ||
}, | ||
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); | ||
} | ||
} | ||
} | ||
}, | ||
// 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' | ||
] | ||
} | ||
} | ||
} | ||
``` | ||
#### options.underscore | ||
Optional | ||
Type: `Boolean` | ||
Default: False | ||
Defines whether Underscore is available in the templates at render time. | ||
### Examples | ||
@@ -313,2 +393,6 @@ | ||
## Release History | ||
### 0.3.8 | ||
* Added support for helper functions. | ||
* Added option to make Underscore JS available in the templates. | ||
### 0.3.7 | ||
@@ -315,0 +399,0 @@ * Added dashed_file_name variable to make easier to include page name into CSS. |
@@ -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 |
@@ -73,4 +73,31 @@ 'use strict'; | ||
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
2584950
125
50840
417
11
1