Created
October 12, 2014 00:46
-
-
Save obenjiro/14281a4a30042074e7a9 to your computer and use it in GitHub Desktop.
function template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tag(tagName) { | |
return function() { | |
var args = arguments; | |
return function () { | |
return '<' + tagName + '>' + evalContent(args) + '</' + tagName + '>'; | |
}; | |
}; | |
} | |
function evalContent(args) { | |
var result = ""; | |
for(var i = 0; i < args.length; i++) { | |
if (typeof args[i] == "function") { | |
result += args[i](); | |
} else { | |
result += args[i]; | |
} | |
} | |
return result; | |
} | |
var _div = tag('div'); | |
var _h1 = tag('h1'); | |
var _span = tag('span'); | |
function index() { | |
return _loop.index; | |
} | |
function _loop(start, count) { | |
return function() { | |
var args = arguments; | |
return function() { | |
var result = ""; | |
for (var i = start; i < count; i++) { | |
_loop.index = i; | |
result += evalContent(args); | |
} | |
return result; | |
}; | |
}; | |
} | |
var template = function(context) { | |
return _loop(0,3)( | |
_div( | |
_h1(context.title), | |
_span(index) | |
) | |
); | |
}; | |
var data = { | |
title: 'test', | |
arr: [1,2,3] | |
}; | |
document.body.innerHTML = template(data)(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment