19
19
20
20
This class contains code based on the following work:
21
21
22
- * Mustache.js version 0.5.1-dev
22
+ * Mustache.js version 0.7.2
23
23
24
24
Code:
25
25
https://github.com/janl/mustache.js
56
56
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57
57
58
58
************************************************************************ */
59
- /* ************************************************************************
60
-
61
- #ignore(module)
62
59
63
- ************************************************************************ */
64
60
/**
65
- * The is a template class which can be used for HTML templating. In fact,
66
- * this is a wrapper for mustache.js which is a "framework-agnostic way to
61
+ * The is a template class which can be used for HTML templating. In fact,
62
+ * this is a wrapper for mustache.js which is a "framework-agnostic way to
67
63
* render logic-free views".
68
- *
64
+ *
69
65
* Here is a basic example how to use it:
70
66
* Template:
71
- * <pre>
67
+ * <pre class="javascript" >
72
68
* var template = "Hi, my name is {{name}}!";
73
69
* var view = {name: "qooxdoo"};
74
- * qx.bom.Template.toHtml (template, view);
70
+ * qx.bom.Template.render (template, view);
75
71
* // return "Hi, my name is qooxdoo!"
76
72
* </pre>
77
- *
73
+ *
78
74
* For further details, please visit the mustache.js documentation here:
79
75
* https://github.com/janl/mustache.js/blob/master/README.md
76
+ *
77
+ * @ignore(module)
80
78
*/
81
79
qx.Bootstrap.define("qx.bom.Template", {
82
80
statics : {
@@ -95,35 +93,60 @@ qx.Bootstrap.define("qx.bom.Template", {
95
93
*/
96
94
render: null,
97
95
96
+ /**
97
+ * Combines {@link #render} and {@link #get}. Input is equal to {@link #render}
98
+ * and output is equal to {@link #get}. The advantage over {@link #get}
99
+ * is that you don't need a HTML template but can use a template
100
+ * string and still get a DOM element. Keep in mind that templates
101
+ * can only have one root element.
102
+ *
103
+ * @param template {String} The String containing the template.
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
+ renderToNode : function(template, view, partials) {
109
+ var renderedTmpl = this.render(template, view, partials);
110
+ return this._createNodeFromTemplate(renderedTmpl);
111
+ },
98
112
99
113
/**
100
- * Helper method which provides you with a direct access to templates
101
- * stored as HTML in the DOM. The DOM node with the given ID will be used
102
- * as a template, parsed and a new DOM node will be returned containing the
103
- * parsed data. Keep in mind to have only one root DOM element in the the
114
+ * Helper method which provides you with a direct access to templates
115
+ * stored as HTML in the DOM. The DOM node with the given ID will be used
116
+ * as a template, parsed and a new DOM node will be returned containing the
117
+ * parsed data. Keep in mind to have only one root DOM element in the the
104
118
* template.
119
+ * Additionally, you should not put the template into a regular, hidden
120
+ * DOM element because the template may not be valid HTML due to the containing
121
+ * mustache tags. We suggest to put it into a script tag with the type
122
+ * <code>text/template</code>.
105
123
*
106
124
* @param id {String} The id of the HTML template in the DOM.
107
125
* @param view {Object} The object holding the data to render.
108
126
* @param partials {Object} Object holding parts of a template.
109
- * @return {DomNode } A DOM element holding the parsed template data.
127
+ * @return {Element } A DOM element holding the parsed template data.
110
128
*/
111
129
get : function(id, view, partials) {
112
130
// get the content stored in the DOM
113
131
var template = document.getElementById(id);
114
- var inner = template.innerHTML;
132
+ return this.renderToNode(template.innerHTML, view, partials);
133
+ },
115
134
116
- // apply the view
117
- inner = this.toHtml(inner, view, partials);
118
-
119
- // special case for text only conversion
120
- if (inner.search(/<|>/) === -1) {
121
- return inner;
135
+ /**
136
+ * Accepts a parsed template and returns a (potentially nested) node.
137
+ *
138
+ * @param template {String} The String containing the template.
139
+ * @return {Element} A DOM element holding the parsed template data.
140
+ */
141
+ _createNodeFromTemplate : function(template) {
142
+ // template is text only (no html elems) so use text node
143
+ if (template.search(/<|>/) === -1) {
144
+ return document.createTextNode(template);
122
145
}
123
146
124
- // create a helper to convert the string into DOM nodes
125
- var helper = qx.bom .Element.create("div");
126
- helper.innerHTML = inner ;
147
+ // template has html elems so convert string into DOM nodes
148
+ var helper = qx.dom .Element.create("div");
149
+ helper.innerHTML = template ;
127
150
128
151
return helper.children[0];
129
152
}
@@ -133,7 +156,9 @@ qx.Bootstrap.define("qx.bom.Template", {
133
156
(function() {
134
157
135
158
/**
136
- * Below is the original mustache.js code. Snapshot date is mentioned in
159
+ * Below is the original mustache.js code. Snapshot date is mentioned in
137
160
* the head of this file.
138
- * @lint ignoreUndefined(module)
161
+ * @ignore(exports)
162
+ * @ignore(define.*)
163
+ * @ignore(module.*)
139
164
*/
0 commit comments