Wt  3.3.8
Classes | Public Types | Public Member Functions | Protected Member Functions | List of all members
Wt::WTemplate Class Reference

A widget that renders an XHTML template. More...

#include <Wt/WTemplate>

Inheritance diagram for Wt::WTemplate:
Inheritance graph
[legend]

Classes

struct  Functions
 A collection of predefined functions. More...
 

Public Types

typedef boost::function< bool(WTemplate *t, const std::vector< WString > &args, std::ostream &result)> Function
 A function type. More...
 
- Public Types inherited from Wt::WObject
typedef void(WObject::* Method) ()
 Typedef for a WObject method without arguments.
 

Public Member Functions

 WTemplate (WContainerWidget *parent=0)
 Creates a template widget.
 
 WTemplate (const WString &text, WContainerWidget *parent=0)
 Creates a template widget with given template. More...
 
virtual WString templateText () const
 Returns the template. More...
 
void setTemplateText (const WString &text, TextFormat textFormat=XHTMLText)
 Sets the template text. More...
 
void setWidgetIdMode (WidgetIdMode mode)
 Sets how the varName should be reflected on bound widgets. More...
 
WidgetIdMode widgetIdMode () const
 Returns how the varName is reflected on a bound widget. More...
 
virtual void bindString (const std::string &varName, const WString &value, TextFormat textFormat=XHTMLText)
 Binds a string value to a variable. More...
 
void bindInt (const std::string &varName, int value)
 Binds an integer value to a variable. More...
 
virtual void bindWidget (const std::string &varName, WWidget *widget)
 Binds a widget to a variable. More...
 
WWidgettakeWidget (const std::string &varName)
 Unbinds a widget. More...
 
void bindEmpty (const std::string &varName)
 Binds an empty string to a variable. More...
 
void addFunction (const std::string &name, const Function &function)
 Binds a function. More...
 
void setCondition (const std::string &name, bool value)
 Sets a condition. More...
 
virtual bool conditionValue (const std::string &name) const
 Returns a condition value. More...
 
std::set< std::string > conditionsSet ()
 Returns the set of conditions set to true.
 
virtual void resolveString (const std::string &varName, const std::vector< WString > &args, std::ostream &result)
 Resolves the string value for a variable name. More...
 
virtual void handleUnresolvedVariable (const std::string &varName, const std::vector< WString > &args, std::ostream &result)
 Handles a variable that could not be resolved. More...
 
virtual WWidgetresolveWidget (const std::string &varName)
 Resolves a widget for a variable name. More...
 
virtual bool resolveFunction (const std::string &name, const std::vector< WString > &args, std::ostream &result)
 Resolves a function call. More...
 
template<typename T >
resolve (const std::string &varName)
 Returns a widget for a variable name. More...
 
virtual void clear ()
 Erases all variable bindings. More...
 
void setInternalPathEncoding (bool enabled)
 Enables internal path anchors in the XHTML template. More...
 
bool hasInternalPathEncoding () const
 Returns whether internal paths are enabled. More...
 
void setEncodeTemplateText (bool on)
 Configures when internal path encoding is done. More...
 
bool encodeTemplateText () const
 Returns whether internal path encoding is done on the template text. More...
 
virtual void refresh ()
 Refresh the widget. More...
 
virtual void renderTemplate (std::ostream &result)
 Renders the template into the given result stream. More...
 
bool renderTemplateText (std::ostream &result, const WString &templateText)
 Renders a template into the given result stream. More...
 
std::string getErrorText ()
 Renders the errors during renderring. More...
 
- Public Member Functions inherited from Wt::WInteractWidget
 WInteractWidget (WContainerWidget *parent=0)
 Create an InteractWidget with optional parent.
 
EventSignal< WKeyEvent > & keyWentDown ()
 Event signal emitted when a keyboard key is pushed down. More...
 
EventSignal< WKeyEvent > & keyPressed ()
 Event signal emitted when a "character" was entered. More...
 
EventSignal< WKeyEvent > & keyWentUp ()
 Event signal emitted when a keyboard key is released. More...
 
EventSignalenterPressed ()
 Event signal emitted when enter was pressed. More...
 
EventSignalescapePressed ()
 Event signal emitted when escape was pressed. More...
 
EventSignal< WMouseEvent > & clicked ()
 Event signal emitted when a mouse key was clicked on this widget. More...
 
EventSignal< WMouseEvent > & doubleClicked ()
 Event signal emitted when a mouse key was double clicked on this widget. More...
 
EventSignal< WMouseEvent > & mouseWentDown ()
 Event signal emitted when a mouse key was pushed down on this widget. More...
 
EventSignal< WMouseEvent > & mouseWentUp ()
 Event signal emitted when a mouse key was released on this widget. More...
 
EventSignal< WMouseEvent > & mouseWentOut ()
 Event signal emitted when the mouse went out of this widget. More...
 
EventSignal< WMouseEvent > & mouseWentOver ()
 Event signal emitted when the mouse entered this widget. More...
 
EventSignal< WMouseEvent > & mouseMoved ()
 Event signal emitted when the mouse moved over this widget. More...
 
EventSignal< WMouseEvent > & mouseDragged ()
 Event signal emitted when the mouse is dragged over this widget. More...
 
EventSignal< WMouseEvent > & mouseWheel ()
 Event signal emitted when the mouse scroll wheel was used. More...
 
EventSignal< WTouchEvent > & touchStarted ()
 Event signal emitted when a finger is placed on the screen. More...
 
EventSignal< WTouchEvent > & touchEnded ()
 Event signal emitted when a finger is removed from the screen. More...
 
EventSignal< WTouchEvent > & touchMoved ()
 Event signal emitted when a finger, which is already placed on the screen, is moved across the screen. More...
 
EventSignal< WGestureEvent > & gestureStarted ()
 Event signal emitted when a gesture is started. More...
 
EventSignal< WGestureEvent > & gestureChanged ()
 Event signal emitted when a gesture is changed. More...
 
EventSignal< WGestureEvent > & gestureEnded ()
 Event signal emitted when a gesture is ended. More...
 
void setDraggable (const std::string &mimeType, WWidget *dragWidget=0, bool isDragWidgetOnly=false, WObject *sourceWidget=0)
 Configure dragging for drag and drop. More...
 
void unsetDraggable ()
 Disable drag & drop for this widget. More...
 
void setMouseOverDelay (int delay)
 Sets a delay for the mouse over event. More...
 
int mouseOverDelay () const
 Returns the mouse over signal delay. More...
 
virtual void setPopup (bool popup)
 Lets the widget overlay over other sibling widgets. More...
 
virtual void load ()
 Loads content just before the widget is used. More...
 
virtual bool isEnabled () const
 Returns whether the widget is enabled. More...
 
- Public Member Functions inherited from Wt::WWebWidget
 WWebWidget (WContainerWidget *parent=0)
 Construct a WebWidget with a given parent. More...
 
virtual void setPositionScheme (PositionScheme scheme)
 Sets the CSS position scheme. More...
 
virtual PositionScheme positionScheme () const
 Returns the CSS position scheme. More...
 
virtual void setOffsets (const WLength &offset, WFlags< Side > sides=All)
 Sets CSS offsets for a non-statically positioned widget. More...
 
virtual WLength offset (Side s) const
 Returns a CSS offset. More...
 
virtual void resize (const WLength &width, const WLength &height)
 Resizes the widget. More...
 
virtual WLength width () const
 Returns the width. More...
 
virtual WLength height () const
 Returns the height. More...
 
virtual void setMinimumSize (const WLength &width, const WLength &height)
 Sets a minimum size. More...
 
virtual WLength minimumWidth () const
 Returns the minimum width. More...
 
virtual WLength minimumHeight () const
 Returns the minimum height. More...
 
virtual void setMaximumSize (const WLength &width, const WLength &height)
 Sets a maximum size. More...
 
virtual WLength maximumWidth () const
 Returns the maximum width. More...
 
virtual WLength maximumHeight () const
 Returns the maximum height. More...
 
virtual void setLineHeight (const WLength &height)
 Sets the CSS line height for contained text.
 
virtual WLength lineHeight () const
 Returns the CSS line height for contained text. More...
 
virtual void setFloatSide (Side s)
 Specifies a CSS float side. More...
 
virtual Side floatSide () const
 Returns the CSS float side. More...
 
virtual void setClearSides (WFlags< Side > sides)
 Sets the sides that should be cleared of floats. More...
 
virtual WFlags< SideclearSides () const
 Returns the sides that should remain empty. More...
 
virtual void setMargin (const WLength &margin, WFlags< Side > sides=All)
 Sets CSS margins around the widget. More...
 
virtual WLength margin (Side side) const
 Returns a CSS margin set. More...
 
virtual void setHiddenKeepsGeometry (bool enabled)
 Sets whether the widget keeps its geometry when hidden. More...
 
virtual bool hiddenKeepsGeometry () const
 Returns whether the widget keeps its geometry when hidden. More...
 
virtual void setHidden (bool hidden, const WAnimation &animation=WAnimation())
 Hides or shows the widget. More...
 
virtual bool isHidden () const
 Returns whether the widget is set hidden. More...
 
virtual bool isVisible () const
 Returns whether the widget is visible. More...
 
virtual void setDisabled (bool disabled)
 Sets whether the widget is disabled. More...
 
virtual bool isDisabled () const
 Returns whether the widget is set disabled. More...
 
virtual bool isPopup () const
 Returns whether the widget is overlayed. More...
 
virtual void setInline (bool isInline)
 Sets whether the widget is displayed inline or as a block. More...
 
virtual bool isInline () const
 Returns whether the widget is displayed inline or as block. More...
 
virtual void setDecorationStyle (const WCssDecorationStyle &style)
 Sets a CSS decoration style. More...
 
virtual WCssDecorationStyledecorationStyle ()
 Returns the decoration style of this widget. More...
 
virtual void setStyleClass (const WString &styleClass)
 Sets (one or more) CSS style classes. More...
 
virtual WString styleClass () const
 Returns the CSS style class. More...
 
virtual void addStyleClass (const WString &styleClass, bool force=false)
 Adds a CSS style class. More...
 
virtual void removeStyleClass (const WString &styleClass, bool force=false)
 Removes a CSS style class. More...
 
virtual bool hasStyleClass (const WString &styleClass) const
 Returns whether the widget has a style class.
 
virtual void setVerticalAlignment (AlignmentFlag alignment, const WLength &length=WLength())
 Sets the vertical alignment. More...
 
virtual AlignmentFlag verticalAlignment () const
 Returns the vertical alignment. More...
 
virtual WLength verticalAlignmentLength () const
 Returns the fixed vertical alignment that was set. More...
 
virtual void setToolTip (const WString &text, TextFormat textFormat=PlainText)
 Sets a tooltip. More...
 
virtual void setDeferredToolTip (bool enable, TextFormat textFormat=PlainText)
 Enable deferred tooltip. More...
 
virtual WString toolTip () const
 Returns the tooltip.
 
virtual void setAttributeValue (const std::string &name, const WString &value)
 Sets an attribute value. More...
 
virtual WString attributeValue (const std::string &name) const
 Returns an attribute value. More...
 
virtual void setJavaScriptMember (const std::string &name, const std::string &value)
 Sets a JavaScript member. More...
 
virtual std::string javaScriptMember (const std::string &name) const
 Returns the value of a JavaScript member. More...
 
virtual void callJavaScriptMember (const std::string &name, const std::string &args)
 Calls a JavaScript member. More...
 
virtual bool loaded () const
 Returns whether this widget has been loaded. More...
 
virtual void setId (const std::string &id)
 Sets the CSS Id. More...
 
virtual WWidgetfind (const std::string &name)
 Finds a descendent widget by name. More...
 
virtual WWidgetfindById (const std::string &id)
 Finds a descendent widget by id.
 
virtual void setSelectable (bool selectable)
 Sets as selectable. More...
 
virtual void doJavaScript (const std::string &javascript)
 Executes the given JavaScript statements when the widget is rendered or updated. More...
 
virtual const std::string id () const
 Returns the (unique) identifier for this object. More...
 
void setLoadLaterWhenInvisible (bool)
 Change the way the widget is loaded when invisible. More...
 
std::string htmlTagName () const
 returns the current html tag name More...
 
void setHtmlTagName (const std::string &tag)
 set the custom HTML tag name More...
 
const std::vector< WWidget * > & children () const
 Returns contained widgets. More...
 
SignalchildrenChanged ()
 Signal emitted when children have been added or removed. More...
 
virtual void setCanReceiveFocus (bool enabled)
 Sets whether the widget can receive focus. More...
 
virtual bool canReceiveFocus () const
 Returns whether the widget can receive focus. More...
 
virtual bool setFirstFocus ()
 Set focus on the widget's first descendant. More...
 
virtual void setFocus (bool focus)
 Sets focus. More...
 
virtual bool hasFocus () const
 Returns whether the widget currently has the focus.
 
virtual void setTabIndex (int index)
 Sets the tab index. More...
 
virtual int tabIndex () const
 Returns the tab index. More...
 
EventSignalblurred ()
 Signal emitted when the widget lost focus. More...
 
EventSignalfocussed ()
 Signal emitted when the widget recieved focus. More...
 
virtual bool scrollVisibilityEnabled () const WT_CXX11ONLY(finaloverride)
 Returns whether scroll visibility detection is enabled for this widget. More...
 
virtual void setScrollVisibilityEnabled (bool enabled) WT_CXX11ONLY(finaloverride)
 Sets whether scroll visibility detection is enabled for this widget. More...
 
virtual int scrollVisibilityMargin () const WT_CXX11ONLY(finaloverride)
 Returns the margin around the viewport within which the widget is considered visible. More...
 
virtual void setScrollVisibilityMargin (int margin) WT_CXX11ONLY(finaloverride)
 Sets the margin around the viewport within which the widget is considered visible. More...
 
virtual Signal< bool > & scrollVisibilityChanged () WT_CXX11ONLY(finaloverride)
 Signal triggered when the scroll visibility of this widget changes. More...
 
virtual bool isScrollVisible () const WT_CXX11ONLY(finaloverride)
 Returns whether this widget is currently considered scroll visible. More...
 
virtual void setThemeStyleEnabled (bool enabled)
 Sets whether theme styling for a widget is enabled or disabled. More...
 
virtual bool isThemeStyleEnabled () const
 Returns whether this widget is currently styled by the chosen theme. More...
 
- Public Member Functions inherited from Wt::WWidget
virtual ~WWidget ()
 Destructor. More...
 
WWidgetparent () const
 Returns the parent widget. More...
 
virtual void removeChild (WObject *child)
 Removes a child object. More...
 
void setWidth (const WLength &width)
 Sets the width. More...
 
void setHeight (const WLength &height)
 Sets the height. More...
 
virtual void positionAt (const WWidget *widget, Orientation orientation=Vertical)
 Positions a widget next to another widget. More...
 
virtual void toggleStyleClass (const WString &styleClass, bool add, bool force=false)
 Toggles a CSS style class.
 
std::string jsRef () const
 Returns a JavaScript expression to the corresponding DOM node. More...
 
void setFocus ()
 Sets focus. More...
 
virtual void acceptDrops (const std::string &mimeType, const WString &hoverStyleClass=WString())
 Sets a mime type to be accepted for dropping. More...
 
virtual void stopAcceptDrops (const std::string &mimeType)
 Indicates that a mime type is no longer accepted for dropping. More...
 
virtual void setObjectName (const std::string &name)
 Sets an object name. More...
 
virtual void htmlText (std::ostream &out)
 Streams the (X)HTML representation. More...
 
bool isRendered () const
 Returns whether the widget is rendered. More...
 
void hide ()
 Hides the widget. More...
 
void animateHide (const WAnimation &animation)
 Hides the widget using an animation. More...
 
void show ()
 Shows the widget. More...
 
void animateShow (const WAnimation &animation)
 Shows the widget using an animation. More...
 
void enable ()
 Enables the widget. More...
 
void disable ()
 Disable thes widget. More...
 
bool layoutSizeAware () const
 Returns whether the widget is layout size aware. More...
 
- Public Member Functions inherited from Wt::WObject
 WObject (WObject *parent=0)
 Create a WObject with a given parent object. More...
 
virtual ~WObject ()
 Destructor. More...
 
virtual std::string objectName () const
 Returns the object name. More...
 
void resetLearnedSlots ()
 Resets learned stateless slot implementations. More...
 
template<class T >
void resetLearnedSlot (void(T::*method)())
 Resets a learned stateless slot implementation. More...
 
template<class T >
WStatelessSlot * implementStateless (void(T::*method)())
 Declares a slot to be stateless and learn client-side behaviour on first invocation. More...
 
template<class T >
WStatelessSlot * implementStateless (void(T::*method)(), void(T::*undoMethod)())
 Declares a slot to be stateless and learn client-side behaviour in advance. More...
 
void isNotStateless ()
 Marks the current function as not stateless. More...
 
template<class T >
WStatelessSlot * implementJavaScript (void(T::*method)(), const std::string &jsCode)
 Provides a JavaScript implementation for a method. More...
 
void addChild (WObject *child)
 Adds a child object. More...
 
const std::vector< WObject * > & children () const
 Returns the children.
 
WObjectparent () const
 Returns the parent object.
 

Protected Member Functions

virtual void applyArguments (WWidget *w, const std::vector< WString > &args)
 Applies arguments to a resolved widget. More...
 
void format (std::ostream &result, const std::string &s, TextFormat textFormat=PlainText)
 Utility method to safely format an XHTML string. More...
 
void format (std::ostream &result, const WString &s, TextFormat textFormat=PlainText)
 Utility method to safely format an XHTML string. More...
 
virtual void enableAjax ()
 Progresses to an Ajax-enabled widget. More...
 
void reset ()
 Notifies the template that it has changed and must be rerendered. More...
 
- Protected Member Functions inherited from Wt::WInteractWidget
virtual void propagateSetEnabled (bool enabled)
 Propagates that a widget was enabled or disabled through children. More...
 
- Protected Member Functions inherited from Wt::WWebWidget
virtual WStatelessSlot * getStateless (Method method)
 On-demand stateless slot implementation. More...
 
virtual void render (WFlags< RenderFlag > flags)
 Renders the widget. More...
 
- Protected Member Functions inherited from Wt::WWidget
void setLayoutSizeAware (bool sizeAware)
 Sets the widget to be aware of its size set by a layout manager. More...
 
virtual void layoutSizeChanged (int width, int height)
 Virtual method that indicates a size change. More...
 
 WWidget (WContainerWidget *parent=0)
 Creates a widget. More...
 
virtual void dropEvent (WDropEvent dropEvent)
 Handles a drop event. More...
 
virtual int boxPadding (Orientation orientation) const
 Returns the widget's built-in padding. More...
 
virtual int boxBorder (Orientation orientation) const
 Returns the widget's built-in border width. More...
 
void scheduleRender (WFlags< RepaintFlag > flags=0)
 Schedules rerendering of the widget. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Wt::WWebWidget
static WString escapeText (const WString &text, bool newlinesToo=false)
 Escape HTML control characters in the text, to display literally (deprecated). More...
 
static std::string & escapeText (std::string &text, bool newlinestoo=false)
 Escape HTML control characters in the text, to display literally (deprecated). More...
 
static bool removeScript (WString &text)
 Remove tags/attributes from text that are not passive (deprecated). More...
 
static std::string jsStringLiteral (const std::string &v, char delimiter= '\'')
 Turn a UTF8 encoded string into a JavaScript string literal. More...
 
- Static Public Member Functions inherited from Wt::WWidget
static WString tr (const char *key)
 Short hand for WString::tr() More...
 
- Static Protected Member Functions inherited from Wt::WObject
static WObjectsender ()
 Returns the sender of the current slot call. More...
 

Detailed Description

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 "$${". Place holder names can contain '_', '-', '.' and alfanumeric characters.

Usage example:

WString userName = ...;
WTemplate *t = new WTemplate();
t->setTemplateText("<div> How old are you, ${friend} ? ${age-input} </div>");
t->bindString("friend", userName, PlainText);
t->bindWidget("age-input", ageEdit_ = new WLineEdit());

The ownership of the widgets bound to a WTemplate widget are transfered to the WTemplate widdget, and they are deleted when the WTemplate is deleted.

There are currently three syntactic constructs defined: variable place holders, functions and conditional blocks.

A. Variable placeholders

${var} defines a placeholder for the variable "var", and gets replaced with whatever is bound to that variable:

Optionally, additional arguments can be specified using the following syntax:

${var arg1="A value" arg2='A second 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.

B. Functions

${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 three functions that are generally useful:

For example, the following template uses the "tr" function to translate the age-label using the "age-label" internationalized key.

WTemplate *t = new WTemplate();
t->addFunction("tr", &WTemplate::Functions::tr);
t->setTemplateText("<div> ${tr:age-label} ${age-input} </div>");
t->bindWidget("age-input", ageEdit_ = new WLineEdit());

C. Conditional blocks

${<cond>} starts a conditional block with a condition name "cond", and must be closed by a balanced ${</cond>}.

For example:

WTemplate *t = new WTemplate();
t->setTemplateText("<div> ${<if-register>} Register ... ${</if-register>}</div>");
t->setCondition("if-register", true);

Conditions are set using setCondition().

The template can return a bound widget using resolve(), which already tries to cast the widget to the proper type.

CSS

This widget does not provide styling, and can be styled using inline or external CSS as appropriate.

Member Typedef Documentation

typedef boost::function<bool(WTemplate *t, const std::vector<WString>& args, std::ostream& result)> Wt::WTemplate::Function

Member Enumeration Documentation

Enumeration that indicates how a widget's ID may be set.

See also
setWidgetIdMode()
Enumerator
SetNoWidgetId 

Do not set the widget ID.

SetWidgetObjectName 

Use setObjectName() to prefix the ID with the varName. This is a safe choice since Wt still guarantees that the IDs are unique.

SetWidgetId 

Use setId() to set the ID as the varName.

Warning
You must be careful that there are no two widgets with the same ID in yor application.

Constructor & Destructor Documentation

Wt::WTemplate::WTemplate ( const WString text,
WContainerWidget parent = 0 
)

Creates a template widget with given template.

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 Wt::XHTMLText textformat.

Member Function Documentation

void Wt::WTemplate::addFunction ( const std::string &  name,
const Function function 
)

Binds a 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 three predefined functions, which can be bound using:

WTemplate *t = ...;
t->addFunction("id", &WTemplate::Functions::id);
t->addFunction("tr", &WTemplate::Functions::tr);
t->addFunction("block", &WTemplate::Functions::block);
void Wt::WTemplate::applyArguments ( WWidget w,
const std::vector< WString > &  args 
)
protectedvirtual

Applies arguments to a resolved widget.

Currently only a class argument is handled, which adds one or more style classes to the widget w, using WWidget::addStyleClass().

void Wt::WTemplate::bindEmpty ( const std::string &  varName)

Binds an empty string to a variable.

If a widget was bound to the variable, it is deleted first.

See also
bindString()
void Wt::WTemplate::bindInt ( const std::string &  varName,
int  value 
)

Binds an integer value to a variable.

See also
bindString()
void Wt::WTemplate::bindString ( const std::string &  varName,
const WString value,
TextFormat  textFormat = XHTMLText 
)
virtual

Binds a string value to a variable.

Each occurrence of the variable within the template will be substituted by its value.

Note
Depending on the textFormat, the value is validated according as for a WText. The default (XHTMLText) filters "active" content, to avoid XSS-based security risks.
See also
bindWidget(), bindInt()
resolveString()
void Wt::WTemplate::bindWidget ( const std::string &  varName,
WWidget widget 
)
virtual

Binds a widget to a variable.

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, and the widget must not yet be bound to another variable.

The widget is reparented to the WTemplate, so that it is deleted when the WTemplate is deleted.

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 0 widget, which will resolve to an empty string.

See also
bindString()
resolveWidget()
void Wt::WTemplate::clear ( )
virtual

Erases all variable bindings.

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()

bool Wt::WTemplate::conditionValue ( const std::string &  name) const
virtual

Returns a condition value.

See also
setCondition()
void Wt::WTemplate::enableAjax ( )
protectedvirtual

Progresses to an Ajax-enabled widget.

This 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.

See also
WApplication::enableAjax()

Reimplemented from Wt::WWebWidget.

bool Wt::WTemplate::encodeTemplateText ( ) const

Returns whether internal path encoding is done on the template text.

See also
setEncodeTemplateText()
void Wt::WTemplate::format ( std::ostream &  result,
const std::string &  s,
TextFormat  textFormat = PlainText 
)
protected

Utility method to safely format an XHTML string.

The string is formatted according to the indicated textFormat. It is recommended to use this method when specializing resolveString() to avoid security risks.

void Wt::WTemplate::format ( std::ostream &  result,
const WString s,
TextFormat  textFormat = PlainText 
)
protected

Utility method to safely format an XHTML string.

The string is formatted according to the indicated textFormat. It is recommended to use this method when specializing resolveString() to avoid security risks.

std::string Wt::WTemplate::getErrorText ( )

Renders the errors during renderring.

See also
renderTemplateText()
void Wt::WTemplate::handleUnresolvedVariable ( const std::string &  varName,
const std::vector< WString > &  args,
std::ostream &  result 
)
virtual

Handles a variable that could not be resolved.

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.
See also
resolveString()
bool Wt::WTemplate::hasInternalPathEncoding ( ) const

Returns whether internal paths are enabled.

See also
setInternalPathEncoding()
void Wt::WTemplate::refresh ( )
virtual

Refresh the widget.

The refresh method is invoked when the locale is changed using WApplication::setLocale() or when the user hit the refresh button.

The widget must actualize its contents in response.

Note
This does *not* rerender the widget! Calling refresh() usually does not have any effect (unless you've reimplemented refresh() to attach to it an effect).

Reimplemented from Wt::WWebWidget.

void Wt::WTemplate::renderTemplate ( std::ostream &  result)
virtual

Renders the template into the given result stream.

The default implementation will call renderTemplateText() with the templateText().

bool Wt::WTemplate::renderTemplateText ( std::ostream &  result,
const WString templateText 
)

Renders a template into the given result stream.

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.

Return: true if rendered successfully.

See also
getErrorText()
void Wt::WTemplate::reset ( )
protected

Notifies the template that it has changed and must be rerendered.

If you update a WTemplate with e.g bindWidget or setCondition, or change the template text, the template will automatically be rerendered.

However, if you create a subclass of WTemplate and override resolveString or resolveWidget, you will have to notify the WTemplate if it has changed with a call to reset().

template<typename T >
T Wt::WTemplate::resolve ( const std::string &  varName)

Returns a widget for a variable name.

This is a convience method, which calls resolveWidget() and casts the result to type T. You may use this method to fetch widgets that have previously been bound using bindWidget().

bool Wt::WTemplate::resolveFunction ( const std::string &  name,
const std::vector< WString > &  args,
std::ostream &  result 
)
virtual

Resolves a function call.

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().

See also
addFunction()
void Wt::WTemplate::resolveString ( const std::string &  varName,
const std::vector< WString > &  args,
std::ostream &  result 
)
virtual

Resolves the string value for a variable name.

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.
See also
renderTemplate()
WWidget * Wt::WTemplate::resolveWidget ( const std::string &  varName)
virtual

Resolves a widget for a variable name.

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 (Wt::WWidget *known = WTemplate::resolveWidget(varName)) {
return known;
} else {
if (varName == "age-input") {
Wt::WWidget *w = new Wt::WLineEdit(); // widget only created when used
bindWidget(varName, w);
return w;
}
}
void Wt::WTemplate::setCondition ( const std::string &  name,
bool  value 
)

Sets a condition.

This enables or disables the inclusion of a conditional block.

The default value of all conditions is false.

void Wt::WTemplate::setEncodeTemplateText ( bool  on)

Configures when internal path encoding is done.

By default, the internal path encoding (if enabled) is done on the template text before placeholders are being resolved. In some rare situations, you may want to postpone the internal path encoding until after placeholders have been resolved, e.g. if a placeholder was used to provide the string for an anchor href.

The default value is true

void Wt::WTemplate::setInternalPathEncoding ( bool  enabled)

Enables internal path anchors in the XHTML template.

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.

See also
WAnchor::setRefInternalPath()
void Wt::WTemplate::setTemplateText ( const WString text,
TextFormat  textFormat = XHTMLText 
)

Sets the template text.

The text must be proper XHTML, and this is checked unless the XHTML is resolved from a message resource bundle or TextFormat is Wt::XHTMLUnsafeText. This behavior is similar to a WText when configured with the Wt::XHTMLText textformat.

Changing the template text does not clear() bound widgets or values.

See also
clear()
void Wt::WTemplate::setWidgetIdMode ( WidgetIdMode  mode)

Sets how the varName should be reflected on bound widgets.

To easily identify a widget in the browser, it may be convenient to reflect the varName to the widget's ID. This options allows you to choose from two methods.

The default value is SetNoWidgetId which does not reflect the varName on the bound widget.

WWidget * Wt::WTemplate::takeWidget ( const std::string &  varName)

Unbinds a widget.

This removes a previously bound widget and unbinds the corresponding variable, effectively undoing the effect of bindWidget().

The widget is not deleted, but returned instead. cpp

virtual WString Wt::WTemplate::templateText ( ) const
virtual

Returns the template.

See also
setTemplateText()
WidgetIdMode Wt::WTemplate::widgetIdMode ( ) const

Returns how the varName is reflected on a bound widget.

See also
setWidgetIdMode()

Generated on Mon Sep 4 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11