laytpl.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. @Name : laytpl v1.2 - 精妙的JavaScript模板引擎
  3. @Author: 贤心
  4. @Date: 2014-10-27
  5. @Site:http://sentsin.com/layui/laytpl
  6. @License:MIT
  7. */
  8. ;!function(){
  9. "use strict";
  10. var config = {
  11. open: '{{',
  12. close: '}}'
  13. };
  14. var tool = {
  15. exp: function(str){
  16. return new RegExp(str, 'g');
  17. },
  18. //匹配满足规则内容
  19. query: function(type, _, __){
  20. var types = [
  21. '#([\\s\\S])+?', //js语句
  22. '([^{#}])*?' //普通字段
  23. ][type || 0];
  24. return exp((_||'') + config.open + types + config.close + (__||''));
  25. },
  26. escape: function(html){
  27. return String(html||'').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&')
  28. .replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;').replace(/"/g, '&quot;');
  29. },
  30. error: function(e, tplog){
  31. var error = 'Laytpl Error:';
  32. typeof console === 'object' && console.error(error + e + '\n'+ (tplog || ''));
  33. return error + e;
  34. }
  35. };
  36. var exp = tool.exp, Tpl = function(tpl){
  37. this.tpl = tpl;
  38. };
  39. Tpl.pt = Tpl.prototype;
  40. window.errors = 0;
  41. //编译模版
  42. Tpl.pt.parse = function(tpl, data){
  43. var that = this, tplog = tpl;
  44. var jss = exp('^'+config.open+'#', ''), jsse = exp(config.close+'$', '');
  45. tpl = tpl.replace(/\s+|\r|\t|\n/g, ' ').replace(exp(config.open+'#'), config.open+'# ')
  46. .replace(exp(config.close+'}'), '} '+config.close).replace(/\\/g, '\\\\')
  47. .replace(/(?="|')/g, '\\').replace(tool.query(), function(str){
  48. str = str.replace(jss, '').replace(jsse, '');
  49. return '";' + str.replace(/\\/g, '') + ';view+="';
  50. })
  51. .replace(tool.query(1), function(str){
  52. var start = '"+(';
  53. if(str.replace(/\s/g, '') === config.open+config.close){
  54. return '';
  55. }
  56. str = str.replace(exp(config.open+'|'+config.close), '');
  57. if(/^=/.test(str)){
  58. str = str.replace(/^=/, '');
  59. start = '"+_escape_(';
  60. }
  61. return start + str.replace(/\\/g, '') + ')+"';
  62. });
  63. tpl = '"use strict";var view = "' + tpl + '";return view;';
  64. //console.log(tpl);
  65. try{
  66. that.cache = tpl = new Function('d, _escape_', tpl);
  67. return tpl(data, tool.escape);
  68. } catch(e){
  69. delete that.cache;
  70. return tool.error(e, tplog);
  71. }
  72. };
  73. Tpl.pt.render = function(data, callback){
  74. var that = this, tpl;
  75. if(!data) return tool.error('no data');
  76. tpl = that.cache ? that.cache(data, tool.escape) : that.parse(that.tpl, data);
  77. console.log()
  78. if(!callback) return tpl;
  79. callback(tpl);
  80. };
  81. var laytpl = function(tpl){
  82. if(typeof tpl !== 'string') return tool.error('Template not found');
  83. return new Tpl(tpl);
  84. };
  85. laytpl.config = function(options){
  86. options = options || {};
  87. for(var i in options){
  88. config[i] = options[i];
  89. }
  90. };
  91. laytpl.v = '1.2';
  92. "function" == typeof define ? define(function() {
  93. return laytpl
  94. }) : "undefined" != typeof exports ? module.exports = laytpl : window.laytpl = laytpl
  95. }();