index.js
/**
 * Repsonsible for Adding and Rendering Views, 
 * @constructor
 * @param {object} Handlebars - pass in Handlebars during instantiation.
 * @param {string} opts.templatePath - the file path to your template folder.
 * @param {string} opts.templateFileType - the file extension of the files in your templatePath.
 * @returns {object} A handle to the public API of this instance.
 */
Repsonsible for Adding and Rendering Views,
@constructor
@param {object} Handlebars - pass in Handlebars during instantiation.
@param {string} opts.templatePath - the file path to your template folder.
@param {string} opts.templateFileType - the file extension of the files in your templatePath.
@returns {object} A handle to the public API of this instance.
Repsonsible for Adding and Rendering Views @constructor @param { object } Handlebars - pass in Handlebars during instantiation . @param { string } opts . templatePath - the file path to your template folder . @param { string } opts . templateFileType - the file extension of the files in your templatePath . @returns { object } A handle to the public API of this instance .
function VanillaHandlebars(Handlebars, opts){
  opts = opts || {};
  var templatePath = opts.templatePath || './',
      templateFileType = opts.templateFileType || 'html';
  if(typeof opts === 'string'){
    templatePath = opts;
  }
  var getFile = require('get-file');
  var views = {},
  el = window || document.body || document.getElementsByTagName('body')[0],
  e = {
    viewLoaded: "VIEW_LOADED"
  };
  (function validatePath(){
    if(templatePath[templatePath.length -1] !== "/"){
      templatePath += "/";
    }
  }());
  function emit(name){
    var evtData = {
      name: name,
      view: views[name]
    };
    if(!window.jQuery){
      var evt = document.createEvent("CustomEvent");
      evt.initCustomEvent(e.viewLoaded, true, true, evtData);
      el.dispatchEvent(evt);
    } else {
      $(el).trigger(e.viewLoaded, evtData);
    }
  };
  function parseFilename(filename, bExtension){
    if(typeof filename !== 'string') throw new Error('Error parsing filename: ' + filename + ' is not a string.');
    var match = filename.match(/([^:\\/]*?)(?:\.([^ :\\/.]*))?$/)
    bExtension = (Number(bExtension) || 0) + 1;
    return match[bExtension];
  }
  function templateLoadFailure(name, err){
    throw new Error("Unable to load " + name + " template. ", err);
  };
  this.register = function(name, renderFn){
    var filename = parseFilename(name),
        filetype = parseFilename(name, true);
    if(typeof name === 'string' && typeof renderFn == 'function'){
      var target = templatePath + filename + "." + (filetype || templateFileType);
      views[filename] = {
        render: renderFn
      };
      getFile(target, function(err, res){
        if(err){
          templateLoadFailure(filename, err);
        } else {
          views[filename].template = Handlebars.compile(res);
          emit(filename);
        }
      });
    }
  };
  this.render = function(name, context){
    var filename = parseFilename(name),
        filetype = parseFilename(name, true);
    var view = views[filename];
    if(typeof view.render == 'function'){
      if(view.template){
        view.render(view.template(context));
      } else {
        var onViewLoaded = function(evt){
          var data = evt.detail;
          if(data.name === filename){
            view.render(view.template(context));
          }
        };
        if(!window.jQuery){
          el.addEventListener(e.viewLoaded, onViewLoaded);
        } else {
          $(el).on(e.viewLoaded, onViewLoaded);
        }
      }
    }
  };
}
function VanillaHandlebars ( Handlebars , opts ) { opts = opts || { } ; var templatePath = opts . templatePath || './' , templateFileType = opts . templateFileType || 'html' ; if ( typeof opts === 'string' ) { templatePath = opts ; } var getFile = require ( 'get-file' ) ; var views = { } , el = window || document . body || document . getElementsByTagName ( 'body' ) [ 0 ] , e = { viewLoaded : "VIEW_LOADED" } ; ( function validatePath ( ) { if ( templatePath [ templatePath . length - 1 ] !== "/" ) { templatePath += "/" ; } } ( ) ) ; function emit ( name ) { var evtData = { name : name , view : views [ name ] } ; if ( ! window . jQuery ) { var evt = document . createEvent ( "CustomEvent" ) ; evt . initCustomEvent ( e . viewLoaded , true , true , evtData ) ; el . dispatchEvent ( evt ) ; } else { $ ( el ) . trigger ( e . viewLoaded , evtData ) ; } } ; function parseFilename ( filename , bExtension ) { if ( typeof filename !== 'string' ) throw new Error ( 'Error parsing filename: ' + filename + ' is not a string.' ) ; var match = filename . match ( / ([^:\\/]*?)(?:\.([^ :\\/.]*))?$ / ) bExtension = ( Number ( bExtension ) || 0 ) + 1 ; return match [ bExtension ] ; } function templateLoadFailure ( name , err ) { throw new Error ( "Unable to load " + name + " template. " , err ) ; } ; this . register = function ( name , renderFn ) { var filename = parseFilename ( name ) , filetype = parseFilename ( name , true ) ; if ( typeof name === 'string' && typeof renderFn == 'function' ) { var target = templatePath + filename + "." + ( filetype || templateFileType ) ; views [ filename ] = { render : renderFn } ; getFile ( target , function ( err , res ) { if ( err ) { templateLoadFailure ( filename , err ) ; } else { views [ filename ] . template = Handlebars . compile ( res ) ; emit ( filename ) ; } } ) ; } } ; this . render = function ( name , context ) { var filename = parseFilename ( name ) , filetype = parseFilename ( name , true ) ; var view = views [ filename ] ; if ( typeof view . render == 'function' ) { if ( view . template ) { view . render ( view . template ( context ) ) ; } else { var onViewLoaded = function ( evt ) { var data = evt . detail ; if ( data . name === filename ) { view . render ( view . template ( context ) ) ; } } ; if ( ! window . jQuery ) { el . addEventListener ( e . viewLoaded , onViewLoaded ) ; } else { $ ( el ) . on ( e . viewLoaded , onViewLoaded ) ; } } } } ; }
Repsonsible for Adding and Rendering Views,
 @constructor
 @param {object} Handlebars - pass in Handlebars during instantiation.
Repsonsible for Adding and Rendering Views @constructor @param { object } Handlebars - pass in Handlebars during instantiation .
// Support legacy use of VanillaHandlebars where opts was just a string. // Dependencies /**
   * View Management via views object
   * @private
   */ /**
   * Using the window or body for event dispatching.
   * @private
   */ /*
   * Named events container.
   * @private
   */ /**
     * @event
     */ /**
   * Emits a custom event on the DOM element stored in the private "el" property of this VanillaHandlebars instance.
   * @function
   * @private
   */ // Native (will not work < IE9) // Use jquery if we have it. Should allow support of old IE. /**
   * Get the path of the path you need
   * @private
   * @param {string} filename - the filename of the template you're working on.
   * @param {boolean} bExtension - true if you want to return "html" out of "index.html", false if you want "index" out of "index.html"
   */ // If bExtension is true, you will get "html" out of "index.min.html" // otherwise you will get "index.min" out of "index.min.html" // Offset the first option which returns the full path. /**
   * @throws Will throw an error if the AJAX request fails to load the requested template from the templatePath
   * @function
   * @private
   */ // Public API. /**
   * Registers a template by name and assigns a render function to it.
   * @public
   * @instance
   * @param {string} name - should be the filename of your partial, plus or minus the file extension.
   * @example
   * // "home", "about", "contact"
   * @param {function} renderFn - function which will be called when you render this template via vanillaHandlebars.render("myTemplate")
   * @callback
   */ // Success /**
   * 
   * @public
   * @instance
   * @param {string} name - should be the filename of your partial, plus or minus the file extension.
   * @example
   * // "home", "about", "contact"
   * @param {string} context - The compiled output data from the loaded Handlebars template after loading
   */ // Todo: xbrowser addEventListener