load-templates
Load templates from file paths, globs or objects, and cache them as normalized objects.
This is a template loader, pure and simple, no rendering or caching.
Currently any of the following input configurations will return a normalized template object with the same root properties:
loader.load(['abc/*.hbs']);
loader.load('abc/*.md');
loader.load({'abc/def.md': {content: 'ghi.', lmn: 'xyz'});
loader.load({'abc/def.md': {content: 'ghi.'}}, {lmn: 'xyz'});
loader.load({path: 'abc/def.md', content: 'ghi.'}, {lmn: 'xyz'});
loader.load({path: 'abc/def.md', content: 'ghi.', lmn: 'xyz'});
loader.load('abc.md', 'def <%= name %>.');
loader.load('abc.md', 'def <%= name %>.', {name: 'Jon Schlinkert'});
Root properties
path
: file path or key to use for the template.data
: data from the parsed template, e.g. front-matter.locals
: locals pass on one of the loader methods. (keeping locals and data seperate allows you to merge however you need to.)content
: the parsed content of the templateorig
: the original content of the template, if parsed
Properties that are not on this list will be moved/copied to the data
object and retained on orig
.
Install
Install with npm:
npm i load-templates --save-dev
Run tests
npm test
API
options
{Object}: Options to initialize loader
with
var loader = require('load-templates');
Expand glob patterns, load, read, parse and normalize files from file paths, strings, objects, or arrays of these types.
key
{String|Object|Array}: Array, object, string or file paths.value
{String|Object}: String of content, file
object with content
property, or locals
if the first arg is a file path.options
{Object}: Options or locals
.returns
{Object}: Normalized file object.
Examples:
Filepaths or arrays of glob patterns.
var temlates = loader.load(['pages/*.hbs']);
var posts = loader.load('posts/*.md');
As strings or objects:
var docs = loader.load({'foo/bar.md': {content: 'this is content.'}}, {foo: 'bar'});
var post = loader.load('abc.md', 'My name is <%= name %>.', {name: 'Jon Schlinkert'});
key
{String}: Glob patterns or file paths.value
{String|Object}: String of content, file
object with content
property, or locals
if the first arg is a file path.options
{Object}: Options or locals
.returns
{Object}: Normalized file object.
Expand glob patterns, load, read, parse and normalize files
from file paths or strings.
patterns
{Object}: Glob patterns or array of filepaths.options
{Object}: Options or locals
returns
{Array}: Array of normalized file objects.
Normalize an array of patterns.
file
{Object}: The object to normalize.options
{Object}: Options or locals
Normalize a template object.
The current working directory to use. Default is process.cwd()
.
Rename the key
of each template loaded using whatever rename function
is defined on the options. path.basename
is the default.
filepath
{String}: The path of the file to read/parse.Options
{Object}: Options or locals
.
Parse the content of each template loaded using whatever parsing function
is defined on the options. fs.readFileSync
is used by default.
file
{Object}: The template object to normalize.Options
{Object}: Options or locals
.
Normalize a template using whatever normalize function is
defined on the options.
Author
Jon Schlinkert
License
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
This file was generated by verb-cli on September 02, 2014.