/ Published in: JavaScript
All glory to the ml function.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* All glory to the ml() @subp Page-like structured array */ function ml(subp,ctx){ var out = '', ctx = ctx||{}, force = ['script'], clean = function(t){return t.replace(/"/g,'"')}, fill = function(t){return t.replace(/{{[^}]*}}/g, function(hld){with(ctx){return (eval(hld))}})}, render_tags = arguments.callee, render_props = function(props){ var out = ''; if (props) for(var p in props){ if(props.hasOwnProperty(p)) out += ' '+p+'="'+clean(props[p])+'"'; } return out; }; for(var i=0,l=subp.length;i<l;i++){ var len = subp[i].length; var name = subp[i][0], props = len>1&&!subp[i][1].pop?subp[i][1]:null, tags = subp[i][len-1].pop?subp[i][len-1]:null; var rtags = (!tags?null:tags.length==1?tags:render_tags(tags,ctx)), rprops = props?render_props(props):''; out += rtags||(force.indexOf(name)>-1)? '<'+name+(rprops?rprops:'')+'>'+(rtags||'')+'</'+name+'>': '<'+name+(rprops?rprops:'')+'/>'; } return fill(out); } // Some testing var ctx = { a:1,b:2 }; var page = [ ['html',[ ['head',[['script',{ type: 'text/javascript', src: 'http://www.pablovidal.org/js/jquery-underscore'}], ['script',{type: 'text/javascript'},['console.log($);']]] ], ['body',[['h1',['ml 0.1']], ['span',{style:'font-style:italic'},['round {{ a }}... maybe {{ b }}... fight']],['br']] ]] ] ]; document.write(ml(page,ctx));
URL: http://www.twitter.com/pablo_PXL