mirror of
https://github.com/wu736139669/vczero.github.io.git
synced 2026-08-05 06:23:38 +00:00
81 lines
3.2 KiB
JavaScript
81 lines
3.2 KiB
JavaScript
/*TMODJS:{}*/
|
|
!function(String) {
|
|
function template(filename, content) {
|
|
return (/string|function/.test(typeof content) ? compile : renderFile)(filename, content);
|
|
}
|
|
function toString(value, type) {
|
|
return "string" != typeof value && (type = typeof value, "number" === type ? value += "" : value = "function" === type ? toString(value.call(value)) : ""),
|
|
value;
|
|
}
|
|
function escapeFn(s) {
|
|
return escapeMap[s];
|
|
}
|
|
function escapeHTML(content) {
|
|
return toString(content).replace(/&(?![\w#]+;)|[<>"']/g, escapeFn);
|
|
}
|
|
function each(data, callback) {
|
|
if (isArray(data)) for (var i = 0, len = data.length; len > i; i++) callback.call(data, data[i], i, data); else for (i in data) callback.call(data, data[i], i);
|
|
}
|
|
function resolve(from, to) {
|
|
var DOUBLE_DOT_RE = /(\/)[^/]+\1\.\.\1/, dirname = ("./" + from).replace(/[^/]+$/, ""), filename = dirname + to;
|
|
for (filename = filename.replace(/\/\.\//g, "/"); filename.match(DOUBLE_DOT_RE); ) filename = filename.replace(DOUBLE_DOT_RE, "/");
|
|
return filename;
|
|
}
|
|
function renderFile(filename, data) {
|
|
var fn = template.get(filename) || showDebugInfo({
|
|
filename: filename,
|
|
name: "Render Error",
|
|
message: "Template not found"
|
|
});
|
|
return data ? fn(data) : fn;
|
|
}
|
|
function compile(filename, fn) {
|
|
if ("string" == typeof fn) {
|
|
var string = fn;
|
|
fn = function() {
|
|
return new String(string);
|
|
};
|
|
}
|
|
var render = cache[filename] = function(data) {
|
|
try {
|
|
return new fn(data, filename) + "";
|
|
} catch (e) {
|
|
return showDebugInfo(e)();
|
|
}
|
|
};
|
|
return render.prototype = fn.prototype = utils, render.toString = function() {
|
|
return fn + "";
|
|
}, render;
|
|
}
|
|
function showDebugInfo(e) {
|
|
var type = "{Template Error}", message = e.stack || "";
|
|
if (message) message = message.split("\n").slice(0, 2).join("\n"); else for (var name in e) message += "<" + name + ">\n" + e[name] + "\n\n";
|
|
return function() {
|
|
return "object" == typeof console && console.error(type + "\n\n" + message), type;
|
|
};
|
|
}
|
|
var cache = template.cache = {}, String = this.String, escapeMap = {
|
|
"<": "<",
|
|
">": ">",
|
|
'"': """,
|
|
"'": "'",
|
|
"&": "&"
|
|
}, isArray = Array.isArray || function(obj) {
|
|
return "[object Array]" === {}.toString.call(obj);
|
|
}, utils = template.utils = {
|
|
$helpers: {},
|
|
$include: function(filename, data, from) {
|
|
return filename = resolve(from, filename), renderFile(filename, data);
|
|
},
|
|
$string: toString,
|
|
$escape: escapeHTML,
|
|
$each: each
|
|
}, helpers = template.helpers = utils.$helpers;
|
|
template.get = function(filename) {
|
|
return cache[filename.replace(/^\.\//, "")];
|
|
}, template.helper = function(name, helper) {
|
|
helpers[name] = helper;
|
|
}, "function" == typeof define ? define(function() {
|
|
return template;
|
|
}) : "undefined" != typeof exports ? module.exports = template : this.template = template;
|
|
}(); |