87,915
社区成员
发帖
与我相关
我的任务
分享
/**
* 格式化输出
* @param {String|Element} id 模板ID或是模板本身(非标识符将识别为模板本身)
* @param {Object} data 格式化的数据
* @param {Object} helper 附加数据(默认为模板对象)
*/
AceTemplate.format = function(id, data, helper)
/**
* 注册模板,如果没有参数则是注册所有script标签模板
* @param {String} id 模板ID
* @param {Element|String} target 模板对象或者是模板字符串,如果没有则默认获取id对应的DOM对象
*/
AceTemplate.register = function(id, target)
/**
* 注销模板
* @param {String} id 模板ID
*/
AceTemplate.unregister = function(id)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../../scripts/ace-template.js"></script>
<title>ace template examples</title>
<style type="text/css">
#log{
width: 600px;
height: 400px;
}
</style>
</head>
<body>
<textarea id="log"></textarea>
<script id="t1" type="text/template">
if (this["title"])
{
#{title}
}
else
{
<b>无</b>
}
</script>
<script>
(function() {
var log = document.getElementById("log");
var list = [
{
input: ["#{this}", "<b>b</b>"],
output: "<b>b</b>"
},
{
input: ["!#{this}", "<b>b</b>"],
output: "<b>b</b>"
},
{
input: ["#{title}#{size}", {
title: "t"
}],
output: "t"
},
{
input: ["#{title}#{size + 2}", {
title: "t"
}],
error: true
},
{
input: ["#{1 + 2 + 3 + 4}"],
output: "10"
},
{
input: ["t1"],
output: "\t\t\t\t<b>无</b>\n"
}
];
var message = [];
for (var i = 0; i < list.length; i++) {
var item = list[i];
try {
var output = AceTemplate.format(item.input[0], item.input[1]);
if (output == item.output) {
message.push("√" + i + "输出结果符合预期。");
} else {
message.push("×" + i + "输出结果不符合预期。-- " + output);
}
} catch(ex) {
if (item.error) {
message.push("√" + i + "异常符合预期。");
} else {
message.push("×" + i + "异常不符合预期。-- " + ex.message);
}
}
}
log.value = message.join("\n");
})();
</script>
</body>
</html>