tinyTmpl
A tiny template engine written with just dozens of lines, simplified underscore template
Install
NPM
npm install tiny-tmpl
Bower
bower install tiny-tmpl
Usage
var tinyTmpl = ;var tmplStr = '<a href="<%= item.url %>"><% if(item.shown) { %><%= item.text %><% } %></a>';var item = url: 'https://github.com/icefox0801/tinyTmpl' shown: true text: 'tiny-tmpl'var compile = ;console; // <a href="https://github.com/icefox0801/tinyTmpl">tiny-tmpl</a>
Source code
You can just simply copy the source code into your javascript, it's less than 40 lines!
var escapes = "'": "'" '\\': '\\' '\r': 'r' '\n': 'n' '\u2028': 'u2028' '\u2029': 'u2029' ; var matcher = '<%=([\\s\\S]+?)%>|<%([\\s\\S]+?)%>|$' 'g'; var escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g; var { return '\\' + escapesmatch; }; { var cursor = 0; var source = "rst+='"; text; source += "';\n"; source = 'with(obj||{}){\n' + source + '}\n'; source = "var tmp,rst='';\n" + source + 'return rst;\n'; return 'obj' source; };