mustache.js.pre 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* ************************************************************************
  2. qooxdoo - the new era of web development
  3. http://qooxdoo.org
  4. Copyright:
  5. 2004-2012 1&1 Internet AG, Germany, http://www.1und1.de
  6. License:
  7. LGPL: http://www.gnu.org/licenses/lgpl.html
  8. EPL: http://www.eclipse.org/org/documents/epl-v10.php
  9. See the LICENSE file in the project's top-level directory for details.
  10. Authors:
  11. * Martin Wittemann (martinwittemann)
  12. ======================================================================
  13. This class contains code based on the following work:
  14. * Mustache.js version 0.8.0
  15. Code:
  16. https://github.com/janl/mustache.js
  17. Copyright:
  18. (c) 2009 Chris Wanstrath (Ruby)
  19. (c) 2010 Jan Lehnardt (JavaScript)
  20. License:
  21. MIT: http://www.opensource.org/licenses/mit-license.php
  22. ----------------------------------------------------------------------
  23. Copyright (c) 2009 Chris Wanstrath (Ruby)
  24. Copyright (c) 2010 Jan Lehnardt (JavaScript)
  25. Permission is hereby granted, free of charge, to any person obtaining
  26. a copy of this software and associated documentation files (the
  27. "Software"), to deal in the Software without restriction, including
  28. without limitation the rights to use, copy, modify, merge, publish,
  29. distribute, sublicense, and/or sell copies of the Software, and to
  30. permit persons to whom the Software is furnished to do so, subject to
  31. the following conditions:
  32. The above copyright notice and this permission notice shall be
  33. included in all copies or substantial portions of the Software.
  34. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  35. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  36. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  37. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  38. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  39. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  40. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  41. ************************************************************************ */
  42. /**
  43. * The is a template class which can be used for HTML templating. In fact,
  44. * this is a wrapper for mustache.js which is a "framework-agnostic way to
  45. * render logic-free views".
  46. *
  47. * Here is a basic example how to use it:
  48. * Template:
  49. * <pre class="javascript">
  50. * var template = "Hi, my name is {{name}}!";
  51. * var view = {name: "qooxdoo"};
  52. * qx.bom.Template.render(template, view);
  53. * // return "Hi, my name is qooxdoo!"
  54. * </pre>
  55. *
  56. * For further details, please visit the mustache.js documentation here:
  57. * https://github.com/janl/mustache.js/blob/master/README.md
  58. *
  59. * @ignore(module)
  60. */
  61. qx.Bootstrap.define("qx.bom.Template", {
  62. statics : {
  63. /** Contains the mustache.js version. */
  64. version: null,
  65. /**
  66. * Original and only template method of mustache.js. For further
  67. * documentation, please visit https://github.com/janl/mustache.js
  68. *
  69. * @signature function(template, view, partials)
  70. * @param template {String} The String containing the template.
  71. * @param view {Object} The object holding the data to render.
  72. * @param partials {Object} Object holding parts of a template.
  73. * @return {String} The parsed template.
  74. */
  75. render: null,
  76. /**
  77. * Combines {@link #render} and {@link #get}. Input is equal to {@link #render}
  78. * and output is equal to {@link #get}. The advantage over {@link #get}
  79. * is that you don't need a HTML template but can use a template
  80. * string and still get a DOM element. Keep in mind that templates
  81. * can only have one root element.
  82. *
  83. * @param template {String} The String containing the template.
  84. * @param view {Object} The object holding the data to render.
  85. * @param partials {Object} Object holding parts of a template.
  86. * @return {Element} A DOM element holding the parsed template data.
  87. */
  88. renderToNode : function(template, view, partials) {
  89. var renderedTmpl = this.render(template, view, partials);
  90. return this._createNodeFromTemplate(renderedTmpl);
  91. },
  92. /**
  93. * Helper method which provides you with a direct access to templates
  94. * stored as HTML in the DOM. The DOM node with the given ID will be used
  95. * as a template, parsed and a new DOM node will be returned containing the
  96. * parsed data. Keep in mind to have only one root DOM element in the the
  97. * template.
  98. * Additionally, you should not put the template into a regular, hidden
  99. * DOM element because the template may not be valid HTML due to the containing
  100. * mustache tags. We suggest to put it into a script tag with the type
  101. * <code>text/template</code>.
  102. *
  103. * @param id {String} The id of the HTML template in the DOM.
  104. * @param view {Object} The object holding the data to render.
  105. * @param partials {Object} Object holding parts of a template.
  106. * @return {Element} A DOM element holding the parsed template data.
  107. */
  108. get : function(id, view, partials) {
  109. // get the content stored in the DOM
  110. var template = document.getElementById(id);
  111. return this.renderToNode(template.innerHTML, view, partials);
  112. },
  113. /**
  114. * Accepts a parsed template and returns a (potentially nested) node.
  115. *
  116. * @param template {String} The String containing the template.
  117. * @return {Element} A DOM element holding the parsed template data.
  118. */
  119. _createNodeFromTemplate : function(template) {
  120. // template is text only (no html elems) so use text node
  121. if (template.search(/<|>/) === -1) {
  122. return document.createTextNode(template);
  123. }
  124. // template has html elems so convert string into DOM nodes
  125. var helper = qx.dom.Element.create("div");
  126. helper.innerHTML = template;
  127. return helper.children[0];
  128. }
  129. }
  130. });
  131. (function() {
  132. // prevent using CommonJS exports object,
  133. // by shadowing global exports object
  134. var exports;
  135. // prevent using AMD compatible loader,
  136. // by shadowing global define function
  137. var define;
  138. /**
  139. * Below is the original mustache.js code. Snapshot date is mentioned in
  140. * the head of this file.
  141. * @ignore(exports)
  142. * @ignore(define.*)
  143. * @ignore(module.*)
  144. * @lint ignoreNoLoopBlock()
  145. */