Public Member Functions | Protected Member Functions

Wt::WTemplate Class Reference

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

#include <Wt/WTemplate>

Inherits Wt::WInteractWidget.

List of all members.

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.
const WStringtemplateText () const
 Returns the template.
void setTemplateText (const WString &text, TextFormat textFormat=XHTMLText)
 Sets the template text.
void bindWidget (const std::string &varName, WWidget *widget)
 Binds a widget to a variable name.
void bindString (const std::string &varName, const WString &value, TextFormat textFormat=XHTMLText)
 Binds a string value to a variable name.
void bindInt (const std::string &varName, int value)
 Binds an integer value to a variable name.
virtual void resolveString (const std::string &varName, const std::vector< WString > &args, std::ostream &result)
 Resolves the string value for a variable name.
virtual void handleUnresolvedVariable (const std::string &varName, const std::vector< WString > &args, std::ostream &result)
 Handles a variable that could not be resolved.
virtual WWidgetresolveWidget (const std::string &varName)
 Resolves a widget for a variable name.
template<typename T >
resolve (const std::string &varName)
 Returns a widget for a variable name.
void clear ()
 Erases all variable bindings.
virtual void refresh ()
 Refreshes the template.

Protected Member Functions

virtual void renderTemplate (std::ostream &result)
 Renders the template into the given result stream.
void format (std::ostream &result, const std::string &s, TextFormat textFormat=PlainText)
 Utility method to safely format an XHTML string.
void format (std::ostream &result, const WString &s, TextFormat textFormat=PlainText)
 Utility method to safely format an XHTML string.
virtual void enableAjax ()
 Progresses to an Ajax-enabled widget.

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

Variable references use a ${varName} syntax to reference the variable "varName". To use a literal "${", use "$${".

You can bind widgets and values to variables using bindWidget(), bindString() or bindInt() or by reimplementing the resolveString() and resolveWidget() methods.

Usage example:

 WString userName = ...;

 WTemplate *t = new WTemplate();
 t->setTemplateText("<div> How old are you, ${friend} ? ${age-input} </div>");

 t->bindString("friend", userName);
 t->bindWidget("age-input", ageEdit_ = new WLineEdit());

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.


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::bindInt ( const std::string &  varName,
int  value 
)

Binds an integer value to a variable name.

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

Binds a string value to a variable name.

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.

See also:
bindWidget(), bindInt()
resolveString()
void Wt::WTemplate::bindWidget ( const std::string &  varName,
WWidget widget 
)

Binds a widget to a variable name.

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

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

Erases all variable bindings.

Removes all strings and deletes all widgets that were previously bound using bindString() and bindWidget().

void Wt::WTemplate::enableAjax (  )  [protected, virtual]

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.

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.

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::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()
void Wt::WTemplate::refresh (  )  [virtual]

Refreshes the template.

This rerenders the template.

Reimplemented from Wt::WWebWidget.

void Wt::WTemplate::renderTemplate ( std::ostream &  result  )  [protected, virtual]

Renders the 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.

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

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 (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;
     }
   }
 }
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()
const WString& Wt::WTemplate::templateText (  )  const [inline]

Returns the template.

See also:
setTemplateText(const WString&)

Generated on Mon Nov 29 2010 08:03:19 for Wt by doxygen 1.7.1