Giter Club home page Giter Club logo

web-template-file-tree's Introduction

Web Template File Tree

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.

Example

Files

  • views/index.html
  • views/widget.html
  • views/comp/cta.html

Script

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

Results

  • fileTree.cache['index'] is the contents of the file views/index.html
  • fileTree.cache['widget'] is the contents of the file views/widget.html
  • fileTree.cache['comp/cta'] is the contents of the file views/comp/cta.html

Subsequent calls to fileTree.load() will update fileTree.cache.

Documentation

Constructor

FileTree(directoryPath, options)

  • directoryPath {String} the path to the directory containing template files
  • options {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.

Instance Methods

load(callback)

  • callback {Function} the callback, taking one error 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.

save(callback)

  • callback {Function} the callback, taking one error 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.

--

web-template-file-tree's People

Contributors

reykjavikingur avatar astrader avatar

Watchers

James Cloos avatar Sean avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.