01: /*
02: * GWT-Ext Widget Library
03: * Copyright(c) 2007-2008, GWT-Ext.
04: * licensing@gwt-ext.com
05: *
06: * http://www.gwt-ext.com/license
07: */
08:
09: package com.gwtext.client.core;
10:
11: import com.google.gwt.core.client.JavaScriptObject;
12: import com.gwtext.client.util.JavaScriptObjectHelper;
13:
14: /**
15: * A template class that supports advanced functionality like autofilling arrays, conditional processing with basic
16: * comparison operators, sub-templates, basic math function support, special built-in template variables, inline code
17: * execution and more. XTemplate also provides the templating mechanism built into {@link com.gwtext.client.widgets.DataView}.
18: */
19: //todo add docs
20: public class XTemplate extends JsObject {
21:
22: private String html;
23:
24: /**
25: * Create a new Template.
26: *
27: * @param html the HTML fragment
28: */
29: public XTemplate(String html) {
30: jsObj = create(html.replaceAll("'", "\""));
31: this .html = html;
32: }
33:
34: /**
35: * Create a new Template.
36: *
37: * @param htmlfrags the HTML fragments
38: */
39: public XTemplate(String[] htmlfrags) {
40: String htmlfrag = "";
41: for (int i = 0; i < htmlfrags.length; i++) {
42: htmlfrag += htmlfrags[i];
43: }
44: html = htmlfrag.replaceAll("'", "\"");
45: jsObj = create(html);
46: }
47:
48: private XTemplate(JavaScriptObject jsObj) {
49: super (jsObj);
50: }
51:
52: public String getHtml() {
53: return html;
54: }
55:
56: private native JavaScriptObject create(String html) /*-{
57: return new $wnd.Ext.XTemplate(html);
58: }-*/;
59:
60: /**
61: * Returns an HTML fragment of this template with the specified values applied. Use this method when the
62: * params are numeric (i.e. {0})
63: *
64: * @param values the param values
65: * @return the html fragment
66: */
67: public String applyTemplate(String[] values) {
68: return applyTemplate(JavaScriptObjectHelper
69: .convertToJavaScriptArray(values));
70: }
71:
72: /**
73: * Returns an HTML fragment of this template with the specified values applied. Use this method when the
74: * params are named (i.e. {foo})
75: *
76: * @param values the param values
77: * @return the html fragment
78: */
79: public String applyTemplate(NameValuePair[] values) {
80: return applyTemplate(NameValuePair.getJsObj(values));
81: }
82:
83: private native String applyTemplate(JavaScriptObject values) /*-{
84: var template = this.@com.gwtext.client.core.JsObject::getJsObj()();
85: return template.applyTemplate(values);
86: }-*/;
87:
88: /**
89: * Compiles the template into an internal function, eliminating the RegEx overhead.
90: */
91: public native void compile() /*-{
92: var template = this.@com.gwtext.client.core.JsObject::getJsObj()();
93: template.compile();
94: }-*/;
95: }
|