001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.core;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.gwtext.client.util.JavaScriptObjectHelper;
013:
014: /**
015: * Represents an HTML fragment template. Templates can be precompiled for greater performance.
016: * <p/>
017: * The list of available inbuild functions for use within templates are :
018: * 'capitalize', 'date', 'ellipsis', 'htmlDecode', 'htmlEncode', 'lowecase', 'stripTags',
019: * 'substr', 'trim', 'undef', 'uppercase', 'usMoney'.
020: */
021: public class Template extends JsObject {
022:
023: private String html;
024:
025: /**
026: * Create a new Template.
027: *
028: * @param html the HTML fragment
029: */
030: public Template(String html) {
031: jsObj = create(html.replaceAll("'", "\""));
032: this .html = html;
033: }
034:
035: public Template(JavaScriptObject jsObj) {
036: super (jsObj);
037: }
038:
039: /**
040: * Create a new Template.
041: *
042: * @param htmlfrags the HTML fragments
043: */
044: public Template(String[] htmlfrags) {
045: String htmlfrag = "";
046: for (int i = 0; i < htmlfrags.length; i++) {
047: htmlfrag += htmlfrags[i];
048: }
049: html = htmlfrag.replaceAll("'", "\"");
050: jsObj = create(html);
051: }
052:
053: public String getHtml() {
054: return html;
055: }
056:
057: private static Template instance(JavaScriptObject jsObj) {
058: return new Template(jsObj);
059: }
060:
061: private native JavaScriptObject create(String html) /*-{
062: return new $wnd.Ext.Template(html);
063: }-*/;
064:
065: public native JavaScriptObject create(JavaScriptObject htmlfrags) /*-{
066: return new $wnd.Ext.Template.apply($wnd.Ext.Template.create, htmlfrags);
067: }-*/;
068:
069: /**
070: * True to disable format functions (defaults to false).
071: *
072: * @param disable true to disable format functions
073: */
074: public native void setDisableFormats(boolean disable) /*-{
075: var template = this.@com.gwtext.client.core.JsObject::getJsObj()();
076: template.disableFormats = disable;
077: }-*/;
078:
079: /**
080: * Returns an HTML fragment of this template with the specified values applied. Use this method when the
081: * params are numeric (i.e. {0})
082: *
083: * @param values the param values
084: * @return the html fragment
085: */
086: public String applyTemplate(String[] values) {
087: return applyTemplate(JavaScriptObjectHelper
088: .convertToJavaScriptArray(values));
089: }
090:
091: /**
092: * Returns an HTML fragment of this template with the specified values applied. Use this method when the
093: * params are named (i.e. {foo})
094: *
095: * @param values the param values
096: * @return the html fragment
097: */
098: public String applyTemplate(NameValuePair[] values) {
099: return applyTemplate(NameValuePair.getJsObj(values));
100: }
101:
102: private native String applyTemplate(JavaScriptObject values) /*-{
103: var template = this.@com.gwtext.client.core.JsObject::getJsObj()();
104: return template.applyTemplate(values);
105: }-*/;
106:
107: /**
108: * Compiles the template into an internal function, eliminating the RegEx overhead.
109: */
110: public native void compile() /*-{
111: var template = this.@com.gwtext.client.core.JsObject::getJsObj()();
112: template.compile();
113: }-*/;
114: }
|