Skip to content

Ruby lib that compiles Mustache templates into pure Javascript

License

Notifications You must be signed in to change notification settings

DanCollinz/mustache-trimmer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mustache Trimmer - Cleanly shaves your mustache into pure JS

Mustache Trimmer is a Ruby library that compiles Mustache templates into pure Javascript functions.

Installation

gem install mustache-trimmer

Usage

require 'mustache/js'
Mustache.to_javascript("Hello {{planet}}")

Compiled JS function:

function (obj) {
  var out, l1, stack, fetch, escape, isFunction;
  out = [];
  stack = [];
  stack.push(obj);
  fetch = function fetch(key) {
    var i, v;
    for (i = stack.length - 1; i >= 0; i -= 1) {
      v = stack[i][key];
      if (v) {
        return v;
      }
    }
  };
  escape = function escape(value) {
    return ('' + value)
      .replace(/&/g, '&')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;')
      .replace(/"/g, '&quot;');
  };
  isFunction = function isFunction(obj) {
    return !!(obj && obj.constructor && obj.call && obj.apply);
  };
  out.push("Hello ");
  l1 = fetch("planet");
  if (isFunction(l1)) {
    l1 = l1();
  }
  out.push(escape(l1));
  return out.join("");
}

Caveats

The compiler is not fully mustache-spec compliant. All of the modules are supported except for the optional lambda section. This module requires a mustache parser to be present at runtime.

Instead of lambdas being passed the raw text of a section, a closure is passed as the first argument. Simply call the function to render the section.

{{#cacheByUserId}}
  Some expensive stuff.
{{/cacheByUserId}}

cacheByUserId: function(section) {
  var key, value;
  key = "user:#{id}";
  if (value = cache[key]) {
    return value;
  } else {
    value = section();
    cache[key] = value;
    return value;
  }
}

Pro Tip

Use the Google Closure Compiler. Its really good at inlining the template helper functions.

window.hello=function(a){var b,c;b=[];c=[];c.push(a);b.push("Hello ");if((a=function(f){var d,e;for(d=c.length-1;d>=0;d-=1)if(e=c[d][f])return e}("planet"))&&a.constructor&&a.call&&a.apply)a=a();b.push((""+a).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;"));return b.join("")};

License

Copyright (c) 2011 Joshua Peek.

Released under the MIT license. See LICENSE for details.

About

Ruby lib that compiles Mustache templates into pure Javascript

Resources

License

Stars

Watchers

Forks

Packages

No packages published