Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

16 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Shamless port of http://github.com/defunkt/mustache
  3. by Jan Lehnardt <jan@apache.org>
  4. Thanks @defunkt for the awesome code
  5. TBD: License
  6. ChangeLog:
  7. - 04.10.2009: Ininitial port at http://devhouseberlin.de/
  8. */
  9. var Mustache = {
  10. name: "mustache.js",
  11. version: "0.1",
  12. debug: true,
  13. stack: " ",
  14. context: {},
  15. to_html: function(view, template) {
  16. return this.render(template, view);
  17. },
  18. render: function(template, view) {
  19. this.stack = this.stack + " ";
  20. // fail fast
  21. if(template.indexOf("{{") == -1) {
  22. return template;
  23. }
  24. this.context = context = this.merge((this.context || {}), view);
  25. var html = this.render_section(template);
  26. // restore context, recursion might have messed it up
  27. // this.context = context;
  28. return this.render_tags(html);
  29. },
  30. merge: function(a, b) {
  31. for(var name in b) {
  32. if(b.hasOwnProperty(name)) {
  33. a[name] = b[name];
  34. }
  35. }
  36. return a;
  37. },
  38. render_section: function(template) {
  39. if(template.indexOf("{{#") == -1) {
  40. return template;
  41. }
  42. var that = this;
  43. return template.replace(/\{\{\#(.+)\}\}\s*(.+)\s*\{\{\/\1\}\}\s*/mg,
  44. function(match, name, content) {
  45. print(match);
  46. print(name);
  47. print(content);
  48. var value = that.find(name);
  49. if(that.is_array(value)) {
  50. return value.map(function(row) {
  51. return that.render(content, row);
  52. }).join('');
  53. } else if(value) {
  54. return that.render(content);
  55. } else {
  56. return "";
  57. }
  58. }
  59. );
  60. },
  61. is_array: function(a) {
  62. return (a &&
  63. typeof a === 'object' &&
  64. a.constructor === Array);
  65. },
  66. render_tags: function(template) {
  67. // values
  68. var that = this;
  69. return template.replace(/\{\{(!|<|\{)?([^\/#]+?)\1?\}\}+/mg,
  70. function (match, operator, name) {
  71. switch(operator) {
  72. case "!": // ignore comments
  73. return match;
  74. // TODO: partials
  75. // case "<": // render partial
  76. // return this.render_partial()
  77. case '{': // the triple mustache is unescaped
  78. return that.find(name);
  79. default: // escape the value
  80. return that.find(name);
  81. }
  82. }, this);
  83. },
  84. find: function(name) {
  85. name = this.trim(name);
  86. // print(this.stack + "find(" + name + ")");
  87. var context = this.context;
  88. if(typeof context[name] === "function") {
  89. return context[name].apply(context);
  90. }
  91. if(context[name] !== undefined) {
  92. return context[name];
  93. }
  94. throw("Can't find " + name + " in " + context);
  95. },
  96. trim: function(s) {
  97. return s.replace(/^\s*|\s*$/g, '');
  98. },
  99. };
  100. var complex = {
  101. header: function() {
  102. return "Colors";
  103. },
  104. item: [
  105. {name: "red", current: true, url: "#Red"},
  106. {name: "green", current: false, url: "#Green"},
  107. {name: "blue", current: false, url: "#Blue"}
  108. ],
  109. link: function() {
  110. var v = this["current"] === true;
  111. // print("link() returns " + v);
  112. return v;
  113. },
  114. list: function() {
  115. var v = this.item.length !== 0;
  116. // print("list() returns " + v);
  117. return v;
  118. },
  119. empty: function() {
  120. var v = this.item.length === 0;
  121. // print("empty() returns " + v);
  122. return v;
  123. }
  124. };
  125. var template = "<h1>{{header}}<\/h1>\n{{#list}}\n <ul>\n {{#item}}\n {{#current}}\n <li><strong>{{name}}<\/strong><\/li>\n {{\/current}}\n {{#link}}\n <li><a href=\"{{url}}\">{{name}}<\/a><\/li>\n {{\/link}}\n {{\/item}}\n <\/ul>\n{{\/list}}\n{{#empty}}\n <p>The list is empty.<\/p>\n{{\/empty}}";
  126. var result = Mustache.to_html(complex, template);
  127. print(result);