This is a utility that reads files from a directory and maintains a cache of file contents keyed off of relative file paths. It also keeps track of modification dates so that content can be reloaded when necessary.
- views/index.html
- views/widget.html
- views/comp/cta.html
var FileTree = require('web-template-file-tree');
var fileTree = new FileTree(__dirname + '/views');
fileTree.load(function(err) {
if (err) {
console.error('unable to load templates:', err);
}
})
fileTree.cache['index']
is the contents of the fileviews/index.html
fileTree.cache['widget']
is the contents of the fileviews/widget.html
fileTree.cache['comp/cta']
is the contents of the fileviews/comp/cta.html
Subsequent calls to fileTree.load()
will update fileTree.cache
.
directoryPath
{String} the path to the directory containing template filesoptions
{Object}extension
{String} (default 'html') the file extension to look for and exclude from cache keys
Instantiates file tree and initializes cache
to empty Object.
callback
{Function} the callback, taking oneerror
argument
Asynchronously updates cache
property with keys corresponding to relative file paths (without extensions)
and values to file contents.
Passes error
argument to callback if unsuccessful.
For efficiency, it uses last modification timestamp to decide whether to re-read file contents into cache
.
callback
{Function} the callback, taking oneerror
argument
Asynchronously writes cache
to directory,
creating files with relative paths corresponding to keys
(without extensions) and file contents corresponding to values.
Passes error
argument to callback if unsuccessful.
--