Wt  4.0.1
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.h>

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

Classes

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

Public Types

typedef TemplateWidgetIdMode WidgetIdMode
 Typedef for enum Wt::TemplateWidgetIdMode.
 
typedef std::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 ()
 Creates a template widget.
 
 WTemplate (const WString &text)
 Creates a template widget with given template. More...
 
virtual WString templateText () const
 Returns the template. More...
 
void setTemplateText (const WString &text, TextFormat textFormat=TextFormat::XHTML)
 Sets the template text. More...
 
void setWidgetIdMode (TemplateWidgetIdMode mode)
 Sets how the varName should be reflected on bound widgets. More...
 
TemplateWidgetIdMode 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=TextFormat::XHTML)
 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, std::unique_ptr< WWidget > widget)
 Binds a widget to a variable. More...
 
template<typename Widget >
Widget * bindWidget (const std::string &varName, std::unique_ptr< Widget > widget)
 Binds a widget to a variable, returning a raw pointer. More...
 
template<typename Widget , typename... Args>
Widget * bindNew (const std::string &varName, Args &&...args)
 Creates a new widget with the given arguments, and binds it, returning a raw pointer. More...
 
std::unique_ptr< WWidgetremoveWidget (const std::string &varName)
 Unbinds a widget by variable name. More...
 
virtual std::unique_ptr< WWidgetremoveWidget (WWidget *widget) override
 Unbinds a widget by widget pointer. 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 >
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 () override
 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 ()
 Create an InteractWidget.
 
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=nullptr, bool isDragWidgetOnly=false, WObject *sourceWidget=nullptr)
 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) override
 Lets the widget overlay over other sibling widgets. More...
 
virtual void load () override
 Loads content just before the widget is used. More...
 
virtual bool isEnabled () const override
 Returns whether the widget is enabled. More...
 
- Public Member Functions inherited from Wt::WWebWidget
 WWebWidget ()
 Construct a WebWidget.
 
virtual std::vector< WWidget * > children () const override
 Returns child widgets. More...
 
SignalchildrenChanged ()
 Signal emitted when children have been added or removed. More...
 
virtual void setPositionScheme (PositionScheme scheme) override
 Sets the CSS position scheme. More...
 
virtual PositionScheme positionScheme () const override
 Returns the CSS position scheme. More...
 
virtual void setOffsets (const WLength &offset, WFlags< Side > sides=AllSides) override
 Sets CSS offsets for a non-statically positioned widget. More...
 
virtual WLength offset (Side s) const override
 Returns a CSS offset. More...
 
virtual void resize (const WLength &width, const WLength &height) override
 Resizes the widget. More...
 
virtual WLength width () const override
 Returns the width. More...
 
virtual WLength height () const override
 Returns the height. More...
 
virtual void setMinimumSize (const WLength &width, const WLength &height) override
 Sets a minimum size. More...
 
virtual WLength minimumWidth () const override
 Returns the minimum width. More...
 
virtual WLength minimumHeight () const override
 Returns the minimum height. More...
 
virtual void setMaximumSize (const WLength &width, const WLength &height) override
 Sets a maximum size. More...
 
virtual WLength maximumWidth () const override
 Returns the maximum width. More...
 
virtual WLength maximumHeight () const override
 Returns the maximum height. More...
 
virtual void setLineHeight (const WLength &height) override
 Sets the CSS line height for contained text.
 
virtual WLength lineHeight () const override
 Returns the CSS line height for contained text. More...
 
virtual void setFloatSide (Side s) override
 Specifies a CSS float side. More...
 
virtual Side floatSide () const override
 Returns the CSS float side. More...
 
virtual void setClearSides (WFlags< Side > sides) override
 Sets the sides that should be cleared of floats. More...
 
virtual WFlags< SideclearSides () const override
 Returns the sides that should remain empty. More...
 
virtual void setMargin (const WLength &margin, WFlags< Side > sides=AllSides) override
 Sets CSS margins around the widget. More...
 
virtual WLength margin (Side side) const override
 Returns a CSS margin set. More...
 
virtual void setHiddenKeepsGeometry (bool enabled) override
 Sets whether the widget keeps its geometry when hidden. More...
 
virtual bool hiddenKeepsGeometry () const override
 Returns whether the widget keeps its geometry when hidden. More...
 
virtual void setHidden (bool hidden, const WAnimation &animation=WAnimation()) override
 Hides or shows the widget. More...
 
virtual bool isHidden () const override
 Returns whether the widget is set hidden. More...
 
virtual bool isVisible () const override
 Returns whether the widget is visible. More...
 
virtual void setDisabled (bool disabled) override
 Sets whether the widget is disabled. More...
 
virtual bool isDisabled () const override
 Returns whether the widget is set disabled. More...
 
virtual bool isPopup () const override
 Returns whether the widget is overlayed. More...
 
virtual void setInline (bool isInline) override
 Sets whether the widget is displayed inline or as a block. More...
 
virtual bool isInline () const override
 Returns whether the widget is displayed inline or as block. More...
 
virtual void setDecorationStyle (const WCssDecorationStyle &style) override
 Sets a CSS decoration style. More...
 
virtual WCssDecorationStyledecorationStyle () override
 Returns the decoration style of this widget. More...
 
virtual void setStyleClass (const WString &styleClass) override
 Sets (one or more) CSS style classes. More...
 
virtual WString styleClass () const override
 Returns the CSS style class. More...
 
virtual void addStyleClass (const WString &styleClass, bool force=false) override
 Adds a CSS style class. More...
 
virtual void removeStyleClass (const WString &styleClass, bool force=false) override
 Removes a CSS style class. More...
 
virtual bool hasStyleClass (const WString &styleClass) const override
 Returns whether the widget has a style class.
 
virtual void setVerticalAlignment (AlignmentFlag alignment, const WLength &length=WLength()) override
 Sets the vertical alignment. More...
 
virtual AlignmentFlag verticalAlignment () const override
 Returns the vertical alignment. More...
 
virtual WLength verticalAlignmentLength () const override
 Returns the fixed vertical alignment that was set. More...
 
virtual void setToolTip (const WString &text, TextFormat textFormat=TextFormat::Plain) override
 Sets a tooltip. More...
 
virtual void setDeferredToolTip (bool enable, TextFormat textFormat=TextFormat::Plain) override
 Enable deferred tooltip. More...
 
virtual WString toolTip () const override
 Returns the tooltip.
 
virtual void setAttributeValue (const std::string &name, const WString &value) override
 Sets an attribute value. More...
 
virtual WString attributeValue (const std::string &name) const override
 Returns an attribute value. More...
 
virtual void setJavaScriptMember (const std::string &name, const std::string &value) override
 Sets a JavaScript member. More...
 
virtual std::string javaScriptMember (const std::string &name) const override
 Returns the value of a JavaScript member. More...
 
virtual void callJavaScriptMember (const std::string &name, const std::string &args) override
 Calls a JavaScript member. More...
 
virtual bool loaded () const override
 Returns whether this widget has been loaded. More...
 
virtual void setId (const std::string &id) override
 Sets the CSS Id. More...
 
virtual WWidgetfind (const std::string &name) override
 Finds a descendent widget by name. More...
 
virtual WWidgetfindById (const std::string &id) override
 Finds a descendent widget by id.
 
virtual void setSelectable (bool selectable) override
 Sets as selectable. More...
 
virtual void doJavaScript (const std::string &javascript) override
 Executes the given JavaScript statements when the widget is rendered or updated. More...
 
virtual const std::string id () const override
 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...
 
virtual void setCanReceiveFocus (bool enabled) override
 Sets whether the widget can receive focus. More...
 
virtual bool canReceiveFocus () const override
 Returns whether the widget can receive focus. More...
 
virtual bool setFirstFocus () override
 Set focus on the widget's first descendant. More...
 
virtual void setFocus (bool focus) override
 Sets focus. More...
 
virtual bool hasFocus () const override
 Returns whether the widget currently has the focus.
 
virtual void setTabIndex (int index) override
 Sets the tab index. More...
 
virtual int tabIndex () const override
 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 finaloverride
 Returns whether scroll visibility detection is enabled for this widget. More...
 
virtual void setScrollVisibilityEnabled (bool enabled) finaloverride
 Sets whether scroll visibility detection is enabled for this widget. More...
 
virtual int scrollVisibilityMargin () const finaloverride
 Returns the margin around the viewport within which the widget is considered visible. More...
 
virtual void setScrollVisibilityMargin (int margin) finaloverride
 Sets the margin around the viewport within which the widget is considered visible. More...
 
virtual Signal< bool > & scrollVisibilityChanged () finaloverride
 Signal triggered when the scroll visibility of this widget changes. More...
 
virtual bool isScrollVisible () const finaloverride
 Returns whether this widget is currently considered scroll visible. More...
 
virtual void setThemeStyleEnabled (bool enabled) finaloverride
 Sets whether theme styling for a widget is enabled or disabled. More...
 
virtual bool isThemeStyleEnabled () const finaloverride
 Returns whether this widget is currently styled by the chosen theme. More...
 
virtual void setObjectName (const std::string &name) override
 Sets an object name. More...
 
- Public Member Functions inherited from Wt::WWidget
virtual ~WWidget ()
 Destructor. More...
 
WWidgetparent () const
 Returns the parent widget.
 
std::unique_ptr< WWidgetremoveFromParent ()
 Removes the widget from its parent. 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=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 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
void addChild (std::unique_ptr< WObject > child)
 Add a child WObject whose lifetime is determined by this WObject.
 
template<typename Child >
Child * addChild (std::unique_ptr< Child > child)
 Add a child WObject, returning a raw pointer. More...
 
std::unique_ptr< WObjectremoveChild (WObject *child)
 Remove a child WObject, so its lifetime is no longer determined by this WObject.
 
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...
 
- Public Member Functions inherited from Wt::Core::observable
 observable ()
 Default constructor.
 
virtual ~observable ()
 Destructor. More...
 
template<typename... Args, typename C >
auto bindSafe (void(C::*method)(Args...))
 Protects a method call against object destruction. More...
 
template<typename... Args, typename C >
auto bindSafe (void(C::*method)(Args...) const) const
 Protects a const method call against object destruction. More...
 
template<typename Function >
auto bindSafe (const Function &function)
 Protects a function against object destruction. More...
 

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=TextFormat::Plain)
 Utility method to safely format an XHTML string. More...
 
void format (std::ostream &result, const WString &s, TextFormat textFormat=TextFormat::Plain)
 Utility method to safely format an XHTML string. More...
 
virtual void enableAjax () override
 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) override
 Propagates that a widget was enabled or disabled through children. More...
 
- Protected Member Functions inherited from Wt::WWebWidget
virtual WStatelessSlot * getStateless (Method method) override
 On-demand stateless slot implementation. More...
 
virtual void render (WFlags< RenderFlag > flags) override
 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 ()
 Creates a widget.
 
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=None)
 Schedules rerendering of the widget. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Wt::WWebWidget
static std::string jsStringLiteral (const std::string &v, char delimiter= '\'')
 Turn a CharEncoding::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...
 

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 = ...;
auto t = std::make_unique<WTemplate>();
t->setTemplateText("<div> How old are you, ${friend} ? ${age-input} </div>");
t->bindString("friend", userName, TextFormat::Plain);
ageEdit_ = t->bindWidget("age-input", std::make_unique<WLineEdit>());

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.

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

C. Conditional blocks

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

For example:

auto t = std::make_unique<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 std::function<bool(WTemplate *t, const std::vector<WString>& args, std::ostream& result)> Wt::WTemplate::Function

Constructor & Destructor Documentation

Wt::WTemplate::WTemplate ( const WString text)

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::TextFormat::XHTML 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()
template<typename Widget , typename... Args>
Widget* Wt::WTemplate::bindNew ( const std::string &  varName,
Args &&...  args 
)

Creates a new widget with the given arguments, and binds it, returning a raw pointer.

This is implemented as:

std::unique_ptr<Widget> w{new Widget(std::forward<Args>(args)...)};
Widget *result = w.get();
bindWidget(varName, std::unique_ptr<WWidget>(std::move(w)));
return result;

This is a useful shorthand for creating and binding a widget in one go.

void Wt::WTemplate::bindString ( const std::string &  varName,
const WString value,
TextFormat  textFormat = TextFormat::XHTML 
)
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 (TextFormat::XHTML) filters "active" content, to avoid XSS-based security risks.
See also
bindWidget(), bindInt()
resolveString()
void Wt::WTemplate::bindWidget ( const std::string &  varName,
std::unique_ptr< 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 nullptr widget, which will resolve to an empty string.

See also
bindString()
resolveWidget()
template<typename Widget >
Widget* Wt::WTemplate::bindWidget ( const std::string &  varName,
std::unique_ptr< Widget >  widget 
)

Binds a widget to a variable, returning a raw pointer.

This is implemented as:

Widget *result = widget.get();
bindWidget(varName, std::unique_ptr<WWidget>(std::move(widget)));
return result;
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 ( )
overrideprotectedvirtual

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 = TextFormat::Plain 
)
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 = TextFormat::Plain 
)
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 ( )
overridevirtual

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.

std::unique_ptr< WWidget > Wt::WTemplate::removeWidget ( const std::string &  varName)

Unbinds a widget by variable name.

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

If this template does not contain a widget for the given varName, nullptr is returned.

std::unique_ptr< WWidget > Wt::WTemplate::removeWidget ( WWidget widget)
overridevirtual

Unbinds a widget by widget pointer.

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

If this template does not contain the given widget, nullptr is returned.

Reimplemented from Wt::WWidget.

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") {
// widget only created when used
return bindWidget(varName, std::make_unique<Wt::WLineEdit>());
}
return nullptr;
}
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 = TextFormat::XHTML 
)

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::TextFormat::UnsafeXHTML. This behavior is similar to a WText when configured with the Wt::TextFormat::XHTML textformat.

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

See also
clear()
void Wt::WTemplate::setWidgetIdMode ( TemplateWidgetIdMode  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.

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

Returns the template.

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

Returns how the varName is reflected on a bound widget.

See also
setWidgetIdMode()

Generated on Thu Oct 19 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11