Skip to content

Instantly share code, notes, and snippets.

@michaljemala
Created February 1, 2013 17:02

Revisions

  1. @spmason spmason revised this gist Apr 18, 2012. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions mustache.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    * $(a.el).html(html);
    * });
    *
    * The module will also automatically use innerShiv (if defined before this module is loaded) to "fix" HTML5 elements in IE
    * The module requires the use of html5shiv or similar if you want to work with HTML5 elements in IE < 9
    *
    * Build the AMD version of mustache from here: https://github.com/janl/mustache.js and register it with requirejs
    *
    @@ -34,7 +34,6 @@
    return {
    templateCache: templateCache,
    rendererCache: rendererCache,
    makeParseHtml: makeParseHtml,
    'load': function (resourceId, require, callback, config) {
    var split = resourceId.split('!'),
    name = split[0],
  2. @spmason spmason revised this gist Apr 18, 2012. 1 changed file with 2 additions and 17 deletions.
    19 changes: 2 additions & 17 deletions mustache.js
    Original file line number Diff line number Diff line change
    @@ -28,22 +28,7 @@
    * Actual plugin code
    **/
    var templateCache = {},
    rendererCache = {},
    parseHtml;

    function makeParseHtml(){
    // innerShiv fixes setting .html() on HTML5 elements
    // And should only be included on the page if we're IE8 or below
    if(window.innerShiv){ // Obviously innerShiv should be included before this plugin is loaded
    return function(html){
    return $(window.innerShiv(html, false));
    };
    }
    return function(html){
    return $(html);
    };
    }
    parseHtml = makeParseHtml();
    rendererCache = {};

    define(['text', 'mustache'], function(text, mustache){
    return {
    @@ -69,7 +54,7 @@
    rendererCache[name] = function(data, partials){
    var html = mustache.to_html(templateCache[name], data, partials);

    return parseHtml(html);
    return $(html);
    };
    }
    callback(rendererCache[name]);
  3. @spmason spmason created this gist Apr 11, 2012.
    82 changes: 82 additions & 0 deletions mustache.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    /**
    * RequireJS mustache plugin
    *
    * usage:
    * require(['ModuleA', 'mustache!myTemplate'], function (ModuleA, myTemplate) {
    * var a = new ModuleA();
    * var html = myTemplate({foo: 'bar'});
    *
    * $(a.el).html(html);
    * });
    *
    * The module will also automatically use innerShiv (if defined before this module is loaded) to "fix" HTML5 elements in IE
    *
    * Build the AMD version of mustache from here: https://github.com/janl/mustache.js and register it with requirejs
    *
    * Configuration:
    * var require = {
    * ... curl configuration ...
    * mustache: {
    * rootUrl: '/js/templates'
    * templateExtension: 'bar' // Default = 'template'
    * }
    * };
    */
    (function (window) {
    'use strict';
    /*
    * Actual plugin code
    **/
    var templateCache = {},
    rendererCache = {},
    parseHtml;

    function makeParseHtml(){
    // innerShiv fixes setting .html() on HTML5 elements
    // And should only be included on the page if we're IE8 or below
    if(window.innerShiv){ // Obviously innerShiv should be included before this plugin is loaded
    return function(html){
    return $(window.innerShiv(html, false));
    };
    }
    return function(html){
    return $(html);
    };
    }
    parseHtml = makeParseHtml();

    define(['text', 'mustache'], function(text, mustache){
    return {
    templateCache: templateCache,
    rendererCache: rendererCache,
    makeParseHtml: makeParseHtml,
    'load': function (resourceId, require, callback, config) {
    var split = resourceId.split('!'),
    name = split[0],
    rootUrl = (config.rootUrl || (config.baseUrl + '/templates/')).replace(/\/\//g, '/'),
    ext = config.templateExtension || '.template',
    fullName = rootUrl + name + ext;

    if(!config.isBuild && rendererCache[name]){
    callback(rendererCache[name]);
    return;
    } else {
    // The text plugin knows how to load files in node, rhino, and the browser, so let it do the hard work
    text.load(fullName, require, function(template){
    if(!rendererCache[name]){
    templateCache[name] = template;

    rendererCache[name] = function(data, partials){
    var html = mustache.to_html(templateCache[name], data, partials);

    return parseHtml(html);
    };
    }
    callback(rendererCache[name]);
    }, config);
    }
    }
    };
    });

    }(typeof window !== 'undefined' ? window : {}));