Mindon.IDEA

Air off, Mind on ~ / Javascript+Golang, Sci, Health… /

Javascript Micro-Templating

This is a modification version of John Resig’s Javascript Micro-Templating

The modifications are mostly about filters for field values.

I like this javascript mico-templating code from John Resig, it’s really simple, easy-to-use.

I’ve had a little utility that I’ve been kicking around for some time now that I’ve found to be quite useful in my JavaScript application-building endeavors. It’s a super-simple templating function that is fast, caches quickly, and is easy to use. I have a couple tricks that I use to make it real fun to mess with.

Here’s the source code to the templating function (a more-refined version of this code will be in my upcoming book Secrets of the JavaScript Ninja):

original code:

// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
  var cache = {};

  this.tmpl = function tmpl(str, data){
    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the result.
    var fn = !/\W/.test(str) ?
      cache[str] = cache[str] ||
        tmpl(document.getElementById(str).innerHTML) :

      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +

        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +

        // Convert the template into pure JavaScript
        str
          .replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',$1,'")
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");

    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
  };
})();

modified code:

// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
  var cache = {};

  // Added by Mindon
  this.tmplCall = {
    nohtml: function(v) {
      return typeof v == 'string'
        ? v.replace(/&/g,'&amp;').replace(/</g,'&lt;')
           .replace(/>/g,'&gt;').replace(/[ ]{2}/g, '&nbsp; ')
        : v;
    }
  , br: function(v) {
      return typeof v == 'string'
        ? v.replace(/\n{2,}/g, '<br/><br/>').replace(/\n/g, '<br/>')
        : v;
    }
  };

  // Updated by mindon@gmail.com Nov. 3, 2011 ( options parameter append )
  this.tmpl = function tmpl(str, data, opt){
    var _t, _f; // added by Mindon

    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the result.
    var fn = !/\W/.test(str) ?
      cache[str] = cache[str] ||
        tmpl(document.getElementById(str).innerHTML, 0, opt) : // updated by Mindon

      // Appended by Mindon
      ( (_t = opt && opt.html ? 1: 'tmplCall.nohtml') && 
        (_f = opt && opt.fields ? '(' +opt.fields +')(d)' : 1) &&
        ((_t===1&&(_t=0)) || (_f===1&&(_f=0))) && 0
        // fields: function(){return {fieldName: handlefn(v){}}}
      ) ||

      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("d",
        "var p=[],print=function(){p.push.apply(p,arguments);}, _f=" + _f +", _t="
          +_t +",_v=function(k,v){" 
          +'return tmplCall.br('
          +(_f?'_f[k]?_f[k](':'') +(_t?'_t(v)':'v') +(_f?'):'
          +(_t?'_t(v)':'v'):'') +")};" + // updated

        // Introduce the data as local variables using with(){}
        "p.push('" +

        // Convert the template into pure JavaScript
        str.replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',_v('$1',d['$1']),'") // updated
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');return p.join('');");

    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
  };
})();

Templates example:

<script type="text/html" id="item_tmpl">
  <div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>">
    <div class="grid_1 alpha right">
      <img class="righted" src="<%=profile_image_url%>"/>
    </div>
    <div class="grid_6 omega contents">
      <p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text%></p>
    </div>
  </div>
</script>

Another with inline script:

<script type="text/html" id="user_tmpl">
  <% for ( var i = 0; i < users.length; i++ ) { %>
    <li><a href="<%=users[i].url%>"><%=users[i].name%></a></li>
  <% } %>
</script>

Comments