laytpl.js 3.2 KB

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