laytpl.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, ' ').replace(exp(config.open+'#'), config.open+'# ')
  44. .replace(exp(config.close+'}'), '} '+config.close).replace(/\\/g, '\\\\')
  45. .replace(/(?="|')/g, '\\').replace(tool.query(), function(str){
  46. str = str.replace(jss, '').replace(jsse, '');
  47. return '";' + str.replace(/\\/g, '') + ';view+="';
  48. })
  49. .replace(tool.query(1), function(str){
  50. var start = '"+(';
  51. if(str.replace(/\s/g, '') === config.open+config.close){
  52. return '';
  53. }
  54. str = str.replace(exp(config.open+'|'+config.close), '');
  55. if(/^=/.test(str)){
  56. str = str.replace(/^=/, '');
  57. start = '"+_escape_(';
  58. }
  59. return start + str.replace(/\\/g, '') + ')+"';
  60. });
  61. tpl = '"use strict";var view = "' + tpl + '";return view;';
  62. try{
  63. that.cache = tpl = new Function('d, _escape_', tpl);
  64. return tpl(data, tool.escape);
  65. } catch(e){
  66. delete that.cache;
  67. return tool.error(e, tplog);
  68. }
  69. };
  70. Tpl.pt.render = function(data, callback){
  71. var that = this, tpl;
  72. if(!data) return tool.error('no data');
  73. tpl = that.cache ? that.cache(data, tool.escape) : that.parse(that.tpl, data);
  74. if(!callback) return tpl;
  75. callback(tpl);
  76. };
  77. var laytpl = function(tpl){
  78. if(typeof tpl !== 'string') return tool.error('Template not found');
  79. return new Tpl(tpl);
  80. };
  81. laytpl.config = function(options){
  82. options = options || {};
  83. for(var i in options){
  84. config[i] = options[i];
  85. }
  86. };
  87. laytpl.v = '1.2.0';
  88. exports('laytpl', laytpl);
  89. });