forked from BaiduFE/BaiduTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,11 @@ | |
* @email [email protected] | ||
*/ | ||
|
||
;(function(){ | ||
;(function(window){ | ||
|
||
//取得浏览器环境的baidu命名空间,非浏览器环境符合commonjs规范exports出去 | ||
var baidu = typeof module === 'undefined' ? (this.baidu = this.baidu || {}) : module.exports; | ||
//修正在nodejs环境下,采用baiduTemplate变量名 | ||
var baidu = typeof module === 'undefined' ? (window.baidu = window.baidu || {}) : module.exports; | ||
|
||
//模板函数(放置于baidu.template命名空间下) | ||
baidu.template = function(str, data){ | ||
|
@@ -22,7 +23,7 @@ | |
var fn = (function(){ | ||
|
||
//判断如果没有document,则为非浏览器环境 | ||
if(!this.document){ | ||
if(!window.document){ | ||
return bt._compile(str); | ||
}; | ||
|
||
|
@@ -206,4 +207,4 @@ | |
return str; | ||
}; | ||
|
||
})(); | ||
})(window); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"/> | ||
<meta name="keywords" content="baidu template" /> | ||
<meta name="description" content="javascript前端模板" /> | ||
<title>test</title> | ||
<script type="text/javascript" src="./baiduTemplate.js"></script> | ||
</head> | ||
<body> | ||
<h3>BaiduTemplate单元测试用例</h3> | ||
<!-- 测试模板1开始 --> | ||
<script id='t:_1234-abcd-1' type="text/template"> | ||
17、定义变量容错:<br> | ||
<%var tttt = value1%> | ||
1111111<%=tttt%> | ||
<br> | ||
|
||
18、定义变量容错:<br> | ||
<%var wwww = value2;%> | ||
1111111<%=wwww%> | ||
<br> | ||
</script> | ||
<!-- 测试模板1结束 --> | ||
|
||
<div id="results"></div> | ||
|
||
<script type="text/javascript"> | ||
window.onload=function(){ | ||
//测试数据,对应每个用例 | ||
var data={ | ||
value1:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>', | ||
value2:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>', | ||
value3:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>', | ||
value4:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>', | ||
value5:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>', | ||
value6:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>', | ||
value7:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span><span style="color:red;">\\\'\"</span>', | ||
value8:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span><span style="color:red;">\\\'\"</span>', | ||
value9:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>', | ||
value10:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>', | ||
value14:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>', | ||
value15:'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>', | ||
value16:['<span style="color:red;">这是value</span>',123,'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>'], | ||
value17:['<span style="color:red;">这是value</span>',123,'<span style="color:red;">http://wangxiao.github.com/BaiduTemplate/</span>'] | ||
}; | ||
|
||
//使用 | ||
var bat=baidu.template; | ||
|
||
//设置分隔符 | ||
//bat.LEFT_DELIMITER='<!'; | ||
//bat.RIGHT_DELIMITER='!>'; | ||
|
||
//设置默认是否转义 | ||
//bat.ESCAPE = false; | ||
|
||
var timestart = new Date().getTime(); | ||
|
||
//输出函数 | ||
var fun=bat('t:_1234-abcd-1',data); | ||
|
||
|
||
var timeend = new Date().getTime(); | ||
|
||
//alert('最大话编译一次时间:'+(timeend-timestart)+'毫秒'); | ||
|
||
timestart = new Date().getTime(); | ||
|
||
//输出HTML | ||
//var html=bat('t:_1234-abcd-1',data); | ||
|
||
timeend = new Date().getTime(); | ||
|
||
//alert('运行时间:'+(timeend-timestart)+'毫秒'); | ||
|
||
//显示结果 | ||
document.getElementById('results').innerHTML=fun; | ||
} | ||
</script> | ||
|
||
</body> | ||
</html> |