1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- ;(function ($) {
- 'use strict'
- var tmpl = function (str, data) {
- var f = !/[^\w\-\.:]/.test(str)
- ? tmpl.cache[str] = tmpl.cache[str] || tmpl(tmpl.load(str))
- : new Function(
- tmpl.arg + ',tmpl',
- 'var _e=tmpl.encode' + tmpl.helper + ",_s='" +
- str.replace(tmpl.regexp, tmpl.func) + "';return _s;"
- )
- return data ? f(data, tmpl) : function (data) {
- return f(data, tmpl)
- }
- }
- tmpl.cache = {}
- tmpl.load = function (id) {
- return document.getElementById(id).innerHTML
- }
- tmpl.regexp = /([\s'\\])(?!(?:[^{]|\{(?!%))*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g
- tmpl.func = function (s, p1, p2, p3, p4, p5) {
- if (p1) {
- return {
- '\n': '\\n',
- '\r': '\\r',
- '\t': '\\t',
- ' ': ' '
- }[p1] || '\\' + p1
- }
- if (p2) {
- if (p2 === '=') {
- return "'+_e(" + p3 + ")+'"
- }
- return "'+(" + p3 + "==null?'':" + p3 + ")+'"
- }
- if (p4) {
- return "';"
- }
- if (p5) {
- return "_s+='"
- }
- }
- tmpl.encReg = /[<>&"'\x00]/g
- tmpl.encMap = {
- '<': '<',
- '>': '>',
- '&': '&',
- '"': '"',
- "'": '''
- }
- tmpl.encode = function (s) {
- return (s == null ? '' : '' + s).replace(
- tmpl.encReg,
- function (c) {
- return tmpl.encMap[c] || ''
- }
- )
- }
- tmpl.arg = 'o'
- tmpl.helper = ",print=function(s,e){_s+=e?(s==null?'':s):_e(s);}" +
- ',include=function(s,d){_s+=tmpl(s,d);}'
- if (typeof define === 'function' && define.amd) {
- define(function () {
- return tmpl
- })
- } else if (typeof module === 'object' && module.exports) {
- module.exports = tmpl
- } else {
- $.tmpl = tmpl
- }
- }(this))
|