Skip to content

Instantly share code, notes, and snippets.

@stryju
Created July 6, 2012 10:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stryju/3059503 to your computer and use it in GitHub Desktop.
Save stryju/3059503 to your computer and use it in GitHub Desktop.
mini-templating engine ( minimold ) - uses {{single_replace}} and {{iterator}}...{{/iterator}} from some ELEMENT
/*
* usage:
*
* tmpl( id_of_template_source:string , [template_data:object] );
*
*/
var minimold = (function(){
"use strict";
var cache = {};
var minimold = function ( key, data ) {
if ( key in cache ) {
var tmpl_string = cache[ key ];
var f = function( data ) {
var i;
var key_regex;
var j;
var subkey;
var subkey_matches;
var subkey_string;
var subkey_regex;
if ( data ) {
for ( i in data ) {
if ( data.hasOwnProperty( i )) {
if ( typeof data[ i ] !== "string" ) {
subkey_string = [];
subkey = [ key, i ].join();
subkey_regex = new RegExp( [ "{{", i, "}}([\\w\\W]+?){{/", i, "}}" ].join( "" ), "m" );
subkey_matches = tmpl_string.match( subkey_regex );
if ( subkey_matches && subkey_matches.length ) {
cache[ subkey ] = subkey_matches[ 1 ];
for ( j in data[ i ] ) {
subkey_string.push( minimold( subkey, data[ i ][ j ]));
}
tmpl_string = tmpl_string.replace( subkey_matches[ 0 ], subkey_string.join( "" ));
}
} else {
key_regex = new RegExp( [ "{{", i, "}}" ].join( "" ), "g" );
tmpl_string = tmpl_string.replace( key_regex, data[ i ].toString());
}
}
}
return tmpl_string.replace( /\{\{.+?\}\}/g , "" );
}
};
if ( data ) {
return f( data );
} else {
return f;
}
} else {
var tmpl_element = document.getElementById( key );
if ( tmpl_element ) {
cache[ key ] = tmpl_element.innerHTML;
return minimold( key, data );
} else {
return "";
}
}
};
return minimold;
})();
window.minimold=function(){function i(e,b){if(e in h){var f=h[e],d=function(c){var a,d,b,g,j;if(c){for(a in c)if(c.hasOwnProperty(a))if("string"!==typeof c[a]){if(j=[],b=[e,a].join(),g=RegExp(["{{",a,"}}([\\w\\W]+?){{/",a,"}}"].join(""),"m"),(g=f.match(g))&&g.length){h[b]=g[1];for(d in c[a])j.push(i(b,c[a][d]));f=f.replace(g[0],j.join(""))}}else f=f.replace("{{"+a+"}}",c[a]);return f.replace(/\{\{.+?\}\}/g,"")}};return b?d(b):d}return(d=document.getElementById(e))?(h[e]=d.innerHTML,i(e,b)):""}var h=
{};return i}();
<div id=output></div>
<script type=text/html id=ajax-cart-tpl>
<section class=ajax:cart>
<h1>Your cart</h1>
<ul class=ajax:list>
{{products}}
<li class="ajax:product {{class}}">
<h2>
<img src="{{img}}" alt width=68 height=96 />
{{name}}
</h2>
<dl>
{{properties}}
<dt>{{key}}
<dd>{{value}}
{{/properties}}
</dl>
{{/products}}
</ul>
<a href=/cart class=to-cart>checkout</a>
</section>
</script>
<script src=minimold.js></script>
<script>
document.getElementById( "output" ).innerHTML = minimold( "ajax-cart-tpl", {
"products":[{
"name":"new product",
"img":"1.jpg",
"class":"new",
"properties":[{
"key":"size",
"value":"16 mm"
},{
"key":"price",
"value":"16,00\u00a0\u20ac"
},{
"key":"color",
"value":null
},{
"key":"quantity",
"value":26
}]
},{
"name":"old product",
"img":"2.jpg",
"properties":[{
"key":"size",
"value":"16 mm"
},{
"key":"price",
"value":"16,00\u00a0\u20ac"
},{
"key":"color",
"value":null
},{
"key":"quantity",
"value":19
}]
}]
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment