This can be used as a helper with [handlebars][], [lodash][], [assemble][], [engine][] or directly as a utility function.
Heads up!
The purpose of this helper is to easily create an array of files to use in templates. This means that the source and dest patterns will need to be known at render time and, as such, this helper expects a dest
path to be defined as a string on the helper options or on the context, otherwise an error is thrown.
var glob = require('{%= name %}');
console.log(glob('*.js', {dest: ''}));
//=> [ <File "example.js">, <File "index.js"> ]
var handlebars = require('handlebars');
handlebars.registerHelper('glob', glob);
Then in templates:
Tip
Get the contents for each file:
var fs = require('fs');
handlebars.registerHelper('read', function(filepath) {
return fs.readFileSync(filepath, 'utf8');
});
Then in templates:
Then:
// compile
var fn = handlebars.compile(tmpl);
// render
console.log(fn());
var template = require('lodash.template');
Then in templates:
<!-- tmpl -->
<% glob("*", {dest: ""}).map(function(item) { %>
<%= item.stem %>
<% }) %>
Then:
// compile
var fn = template(tmpl, {imports: {glob: glob}});
// render
console.log(fn());