Skip to content

Latest commit

 

History

History
82 lines (60 loc) · 1.47 KB

.verb.md

File metadata and controls

82 lines (60 loc) · 1.47 KB

Usage

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"> ]

Handlebars usage

var handlebars = require('handlebars');
handlebars.registerHelper('glob', glob);

Then in templates:

{{#each (glob "*") as |file|}}
  {{file.path}}
{{/each}}

Tip

Get the contents for each file:

var fs = require('fs');
handlebars.registerHelper('read', function(filepath) {
  return fs.readFileSync(filepath, 'utf8');
});

Then in templates:

<!-- tmpl -->
{{#each (glob "*") as |file|}}
<p>{{read file.path}}</p>
{{/each}}

Then:

// compile
var fn = handlebars.compile(tmpl);
// render 
console.log(fn());

Lo-dash usage

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());