|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objecteu.webtoolkit.jwt.WObject
eu.webtoolkit.jwt.WWidget
eu.webtoolkit.jwt.WWebWidget
eu.webtoolkit.jwt.WInteractWidget
eu.webtoolkit.jwt.WTemplate
public class WTemplate
A widget that renders an XHTML template.
The XHTML template may contain references to variables which replaced by strings are widgets.
Since the template text may be supplied by a WString, you can
conveniently store the string in a message resource bundle, and make it
localized by using WString#tr().
Placeholders (for variables and functions) are delimited by:
${...}. To use a literal "${", use
"$${".
There are currently two syntactic constructs defined.
${var} defines a placeholder for the variable "var",
and gets replaced with whatever is bound to that variable:
bindWidget()bindString() or bindInt()resolveString() and resolveWidget() methods.Optionally, additional arguments can be specified using the following syntax:
${var arg1=value1 arg2="A second value" arg3='A third value'}
The arguments can thus be simple simple strings or quoted strings (single or
double quoted). These arguments are applied to a resolved widget in
applyArguments() and
currently supports only style classes.
You can bind widgets and values to variables using
bindWidget(),
bindString() or bindInt() or by reimplementing the
resolveString() and resolveWidget() methods.
Note: The use of XML comments (<!-- ... -.) around
variables that are bound to widgets will result in bad behaviour since the
template parser is ignorant about these comments and the corresponding
widgets will believe that they are rendered but aren't actually. We are
planning to add a syntax for conditional sections
${fun:arg} defines a placeholder for applying a function
"fun" to an argument "arg".
Optionally, additional arguments can be specified as with a variable placeholder.
Functions are resolved by
resolveFunction(), and the default implementation considers functions bound
with addFunction(). There are currently two functions that are generally useful:
resolves the id of a bound widget. This is convenient to bind
<label> elements to a form widget using its for attribute.
${<cond>} starts a conditional block with a condition name
"cond", and must be closed by a balanced
${</cond>}.
Conditions are set using
setCondition().
WString userName = ...;
WTemplate *t = new WTemplate();
t.setTemplateText(" How old are you, ${friend} ? ${age-input} ");
t.bindString("friend", userName, PlainText);
t.bindWidget("age-input", ageEdit_ = new WLineEdit());
This widget does not provide styling, and can be styled using inline or external CSS as appropriate.
| Constructor Summary | |
|---|---|
WTemplate()
Creates a template widget. |
|
WTemplate(java.lang.CharSequence text)
Creates a template widget with given template. |
|
WTemplate(java.lang.CharSequence text,
WContainerWidget parent)
Creates a template widget with given template. |
|
WTemplate(WContainerWidget parent)
Creates a template widget. |
|
| Method Summary | |
|---|---|
void |
addFunction(java.lang.String name,
eu.webtoolkit.jwt.WTemplate.Function function)
Binds a function. |
protected void |
applyArguments(WWidget w,
java.util.List<WString> args)
Applies arguments to a resolved widget. |
void |
bindEmpty(java.lang.String varName)
Binds an empty string to a variable. |
void |
bindInt(java.lang.String varName,
int value)
Binds an integer value to a variable. |
void |
bindString(java.lang.String varName,
java.lang.CharSequence value)
Binds a string value to a variable. |
void |
bindString(java.lang.String varName,
java.lang.CharSequence value,
TextFormat textFormat)
Binds a string value to a variable. |
void |
bindWidget(java.lang.String varName,
WWidget widget)
Binds a widget to a variable. |
void |
clear()
Erases all variable bindings. |
boolean |
conditionValue(java.lang.String name)
Returns a condition value. |
protected void |
enableAjax()
Progresses to an Ajax-enabled widget. |
protected void |
format(java.io.Writer result,
java.lang.CharSequence s)
Utility method to safely format an XHTML string. |
protected void |
format(java.io.Writer result,
java.lang.CharSequence s,
TextFormat textFormat)
Utility method to safely format an XHTML string. |
protected void |
format(java.io.Writer result,
java.lang.String s)
Utility method to safely format an XHTML string. |
protected void |
format(java.io.Writer result,
java.lang.String s,
TextFormat textFormat)
Utility method to safely format an XHTML string. |
WString |
getTemplateText()
Returns the template. |
void |
handleUnresolvedVariable(java.lang.String varName,
java.util.List<WString> args,
java.io.Writer result)
Handles a variable that could not be resolved. |
boolean |
hasInternalPathEncoding()
Returns whether internal paths are enabled. |
void |
refresh()
Refreshes the template. |
protected void |
renderTemplate(java.io.Writer result)
Renders the template into the given result stream. |
boolean |
resolveFunction(java.lang.String name,
java.util.List<WString> args,
java.io.Writer result)
Resolves a function call. |
void |
resolveString(java.lang.String varName,
java.util.List<WString> args,
java.io.Writer result)
Resolves the string value for a variable name. |
WWidget |
resolveWidget(java.lang.String varName)
Resolves a widget for a variable name. |
void |
setCondition(java.lang.String name,
boolean value)
Sets a condition. |
void |
setInternalPathEncoding(boolean enabled)
Enables internal path anchors in the XHTML template. |
void |
setTemplateText(java.lang.CharSequence text)
Sets the template text. |
void |
setTemplateText(java.lang.CharSequence text,
TextFormat textFormat)
Sets the template text. |
| Methods inherited from class eu.webtoolkit.jwt.WInteractWidget |
|---|
clicked, doubleClicked, enterPressed, escapePressed, gestureChanged, gestureEnded, gestureStarted, isEnabled, keyPressed, keyWentDown, keyWentUp, load, mouseDragged, mouseMoved, mouseWentDown, mouseWentOut, mouseWentOver, mouseWentUp, mouseWheel, propagateSetEnabled, remove, setDraggable, setDraggable, setDraggable, setDraggable, touchEnded, touchMoved, touchStarted |
| Methods inherited from class eu.webtoolkit.jwt.WWidget |
|---|
acceptDrops, acceptDrops, addCssRule, addCssRule, addStyleClass, animateHide, animateShow, boxBorder, boxPadding, containsExposed, disable, dropEvent, enable, getDrop, getJsRef, getParent, hide, htmlText, isLayoutSizeAware, layoutSizeChanged, positionAt, positionAt, removeStyleClass, resize, setClearSides, setHeight, setHidden, setLayoutSizeAware, setMargin, setMargin, setMargin, setMargin, setMargin, setOffsets, setOffsets, setOffsets, setOffsets, setOffsets, setToolTip, setVerticalAlignment, setWidth, show, stopAcceptDrops, toggleStyleClass, toggleStyleClass, tr |
| Methods inherited from class eu.webtoolkit.jwt.WObject |
|---|
getObjectName, setObjectName |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public WTemplate(WContainerWidget parent)
public WTemplate()
Calls this((WContainerWidget)null)
public WTemplate(java.lang.CharSequence text,
WContainerWidget parent)
The templateText must be proper XHTML, and this is checked
unless the XHTML is resolved from a message resource bundle. This
behavior is similar to a WText when configured with the
TextFormat.XHTMLText textformat.
public WTemplate(java.lang.CharSequence text)
Calls this(text, (WContainerWidget)null)
| Method Detail |
|---|
public WString getTemplateText()
setTemplateText(CharSequence text, TextFormat textFormat)
public void setTemplateText(java.lang.CharSequence text,
TextFormat textFormat)
The text must be proper XHTML, and this is checked unless
the XHTML is resolved from a message resource bundle or TextFormat is
TextFormat.XHTMLUnsafeText. This behavior is similar to a
WText when configured with the TextFormat.XHTMLText
textformat.
Changing the template text does not clear()
bound widgets or values.
clear()public final void setTemplateText(java.lang.CharSequence text)
Calls setTemplateText(text, TextFormat.XHTMLText)
public void bindString(java.lang.String varName,
java.lang.CharSequence value,
TextFormat textFormat)
Each occurrence of the variable within the template will be substituted by its value.
Depending on the textFormat, the value is
validated according as for a WText.
bindWidget(String varName, WWidget widget),
bindInt(String varName, int value),
resolveString(String varName, List args, Writer result)
public final void bindString(java.lang.String varName,
java.lang.CharSequence value)
Calls
bindString(varName, value, TextFormat.XHTMLText)
public void bindInt(java.lang.String varName,
int value)
bindString(String varName, CharSequence value, TextFormat
textFormat)
public void bindWidget(java.lang.String varName,
WWidget widget)
The corresponding variable reference within the template will be replaced
with the widget (rendered as XHTML). Since a single widget may be
instantiated only once in a template, the variable varName
may occur at most once in the template.
If a widget was already bound to the variable, it is deleted first. If previously a string or other value was bound to the variable, it is removed.
You may also pass a null widget, which will
resolve to an empty string.
bindString(String varName, CharSequence value, TextFormat
textFormat),
resolveWidget(String varName)public void bindEmpty(java.lang.String varName)
bindString(String varName, CharSequence value, TextFormat
textFormat)
public void addFunction(java.lang.String name,
eu.webtoolkit.jwt.WTemplate.Function function)
Functions are useful to automatically resolve placeholders.
The syntax for a function 'fun' applied to a single argument 'bla' is:
${fun:bla}
There are two predefined functions, which can be bound using:
WTemplate *t = ...;
t.addFunction("id", &WTemplate::Functions::id);
t.addFunction("tr", &WTemplate::Functions::tr);
public void setCondition(java.lang.String name,
boolean value)
This enables or disables the inclusion of a conditional block.
The default value of all conditions is false.
public boolean conditionValue(java.lang.String name)
setCondition(String name, boolean value)
public void resolveString(java.lang.String varName,
java.util.List<WString> args,
java.io.Writer result)
throws java.io.IOException
This is the main method used to resolve variables in the template text, during rendering.
The default implementation considers first whether a string was bound
using
bindString(). If so, that string is returned. If not, it will attempt to
resolve a widget with that variable name using
resolveWidget(), and
render it as XHTML. If that fails too,
handleUnresolvedVariable() is called, passing the initial arguments.
You may want to reimplement this method to provide on-demand loading of strings for your template.
The result stream expects a UTF-8 encoded string value.
Warning:When specializing this class, you need to make sure
that you append proper XHTML to the result, without unsafe
active contents. The
format() methods may be used for this purpose.
java.io.IOExceptionrenderTemplate(Writer result)
public void handleUnresolvedVariable(java.lang.String varName,
java.util.List<WString> args,
java.io.Writer result)
throws java.io.IOException
This method is called from
resolveString() for variables that could not be resolved.
The default implementation implementation writes "??" + varName + "??" to the result stream.
The result stream expects a UTF-8 encoded string value.
Warning:When specializing this class, you need to make sure
that you append proper XHTML to the result, without unsafe
active contents. The
format() methods may be used for this purpose.
java.io.IOExceptionresolveString(String varName, List args, Writer result)public WWidget resolveWidget(java.lang.String varName)
The default implementation returns a widget that was bound using
bindWidget().
You may want to reimplement this method to create widgets on-demand. All
widgets that are returned by this method are reparented to the
WTemplate, so they will be deleted when the template is
destroyed, but they are not deleted by clear()
(unless bind was called on them as in the example below).
This method is typically used for delayed binding of widgets. Usage example:
{
if (Widget *known = WTemplate::resolveWidget(varName)) {
return known;
} else {
if (varName == "age-input") {
WWidget *w = new WLineEdit(); // widget only created when used
bind(varName, w);
return w;
}
}
}
public boolean resolveFunction(java.lang.String name,
java.util.List<WString> args,
java.io.Writer result)
throws java.io.IOException
This resolves a function with name name, and one or more
arguments args, and writes the result into the stream
result. The method returns whether a function was matched
and applied.
The default implementation considers functions that were bound using
addFunction().
java.io.IOExceptionaddFunction(String name, Function function)public void clear()
Removes all strings and deletes all widgets that were previously bound
using
bindString() and
bindWidget().
This also resets all conditions set using
setCondition()
, but does not remove functions added with
addFunction()
public void setInternalPathEncoding(boolean enabled)
Anchors to internal paths are represented differently depending on the
session implementation (plain HTML, Ajax or HTML5 history). By enabling
this option, anchors which reference an internal path (by referring a URL
of the form href="#/..."), are re-encoded to link
to the internal path.
The default value is false.
WAnchor.setRefInternalPath(String path)public boolean hasInternalPathEncoding()
setInternalPathEncoding(boolean enabled)public void refresh()
This rerenders the template.
refresh in class WWebWidget
protected void renderTemplate(java.io.Writer result)
throws java.io.IOException
The default implementation will parse the template, and resolve variables
by calling
resolveString().
You may want to reimplement this method to manage resources that are needed to load content on-demand (e.g. database objects), or support a custom template language.
java.io.IOException
protected void applyArguments(WWidget w,
java.util.List<WString> args)
Currently only a class argument is handled, which adds one
or more style classes to the widget w, using
WWidget#addStyleClass().
protected void format(java.io.Writer result,
java.lang.String s,
TextFormat textFormat)
throws java.io.IOException
The string is formatted according to the indicated
textFormat. It is recommended to use this method when
specializing
resolveString() to avoid security risks.
java.io.IOException
protected final void format(java.io.Writer result,
java.lang.String s)
throws java.io.IOException
Calls format(result, s, TextFormat.PlainText)
java.io.IOException
protected void format(java.io.Writer result,
java.lang.CharSequence s,
TextFormat textFormat)
throws java.io.IOException
The string is formatted according to the indicated
textFormat. It is recommended to use this method when
specializing
resolveString() to avoid security risks.
java.io.IOException
protected final void format(java.io.Writer result,
java.lang.CharSequence s)
throws java.io.IOException
Calls format(result, s, TextFormat.PlainText)
java.io.IOExceptionprotected void enableAjax()
WWidgetThis method is called when the progressive bootstrap method is used, and support for AJAX has been detected. The default behavior will upgrade the widget's event handling to use AJAX instead of full page reloads, and propagate the call to its children.
You may want to reimplement this method if you want to make changes to widget when AJAX is enabled. You should always call the base implementation.
enableAjax in class WWebWidgetWApplication.enableAjax()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||