Wt (web toolkit)#Code example
{{Short description|C++ web application framework}}
{{Infobox software
| name = Wt
| logo =
| screenshot =
| caption =
| author = Emweb
| developer =
| released = 1.0.0 / {{release date and age|2005|12|}}
| latest release version = 4.10.4
| latest release date = {{release date and age|2024|03|06}}{{Cite web |title=Wt: Release notes |url=https://www.webtoolkit.eu/wt/doc/reference/html/Releasenotes.html |access-date=2024-07-10 |website=www.webtoolkit.eu}}
| latest preview version =
| latest preview date =
| operating system = Cross-platform
| platform =
| language =
| genre = Web framework
| repo = {{URL|https://github.com/emweb/wt|Wt Repository}}
| programming language = C++
| license = Dual-licensed:{{Plain list|
- GPL-2.0-with-openSSL-exception{{cite web |url=https://github.com/emweb/wt/blob/master/LICENSE |title=wt/LICENSE |access-date=2024-12-30}}
- [http://www.webtoolkit.eu/wt/download Commercial License]}}
| website = {{url|https://www.webtoolkit.eu/wt}}
}}
Wt (pronounced "witty") is an open-source widget-centric web framework for the C++ programming language. It has an API resembling that of Qt framework (although it was developed with Boost, and is incompatible when mixed with Qt), also using a widget-tree and an event-driven signal/slot system.{{cite web |url=http://www.drdobbs.com/web-development/wt-a-web-toolkit/206401952 |title=Wt: A Web Toolkit |last1=Dumon |first1=Wim |last2=Deforche |first2=Koen |date=February 11, 2008 |website=Dr. Dobb's Journal |access-date=January 24, 2017}}
The Wt's design goal is to benefit from the stateful component model used in desktop-applications APIs, applied to web development—instead of the traditional MVC (model–view–controller) design pattern. So rather than using MVC at the level of a web page, it is pushed to the level of individual components.{{cite web |url=http://www.codeguru.com/cpp/i-n/internet/browsercontrol/article.php/c15275/ |title=Wt: C++ Web Toolkit Library Lets You Write Scripting-Independent Web Apps |last1=Volkman |first1=Victor |date=June 6, 2008 |publisher=QuinStreet |access-date=January 24, 2017}}
While the library uses a desktop software development process, it does support some web-specific features, including:
- Semantic URLs
- Navigation of browser's history
One of the unique features of Wt is its abstraction layer of the browser rendering model. The library uses Ajax for communicating with browsers compatible with it, while using plain HTML-form post-backs for other user agents. Using a progressive bootstrap-method, the user interface is rendered as a plain HTML document first, then, provided its support in browser, it is automatically upgraded to use Ajax for increased interactivity. In this way, Wt is by definition:
- The only server-side framework implementing the strategy of progressive enhancement automatically;
- The only Ajax framework with search engine optimization (SEO) qualities.
Because of the popularity of C/C++ in embedded system environments, Wt is often used in such devices and (as a consequence) has been highly optimized for performance.
Major features
- Automatic graceful degradation and progressive enhancement
- Supports server-initiated events (Comet)
- A unified rendering API (SVG, the HTML5 canvas element, VML)
- Client- and server-side validation
- Contains various security features to avoid Cross-site scripting and Cross-site request forgery (CSRF) vulnerabilities
- Includes a compact C++ ORM-layer ("Wt::Dbo")
- Uses the WebSocket networking protocol, if available, for Client–server model of communication, with fallbacks to Ajax or plain web page rendering
For a more detailed overview, see the Features section of official website.
Code example
The "Hello, World!" program in Wt:
- include
- include
- include
- include
- include
- include
/*
* A simple hello world application class which demonstrates how to react
* to events, read input, and give feed-back.
*/
class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env);
private:
Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
void greet();
};
/*
* The env argument contains information about the new session, and
* the initial request. It must be passed to the WApplication
* constructor so it is typically also an argument for your custom
* application constructor.
- /
HelloApplication::HelloApplication(const Wt::WEnvironment& env)
: WApplication(env)
{
setTitle("Hello world"); // application title
root()->addNew
nameEdit_ = root()->addNew
nameEdit_->setFocus(); // give focus
auto button = root()->addNew
button->setMargin(5, Wt::Side::Left); // add 5 pixels margin
root()->addNew
greeting_ = root()->addNew
/*
* Connect signals with slots
*
* - simple Wt-way: specify object and method
*/
button->clicked().connect(this, &HelloApplication::greet);
/*
* - using an arbitrary function object, e.g. useful to bind
* values with std::bind() to the resulting method call
*/
nameEdit_->enterPressed().connect(std::bind(&HelloApplication::greet, this));
/*
* - using a lambda:
*/
button->clicked().connect([=]() {
std::cerr << "Hello there, " << nameEdit_->text() << "\n";
});
}
void HelloApplication::greet()
{
/*
* Update the text, using text input into the nameEdit_ field.
*/
greeting_->setText("Hello there, " + nameEdit_->text());
}
int main(int argc, char **argv)
{
/*
* Your main method may set up some shared resources, but should then
* start the server application (FastCGI or httpd) that starts listening
* for requests, and handles all of the application life cycles.
*
* The last argument to WRun specifies the function that will instantiate
* new application objects. That function is executed when a new user surfs
* to the Wt application, and after the library has negotiated browser
* support. The function should return a newly instantiated application
* object.
*/
return Wt::WRun(argc, argv, [](const Wt::WEnvironment &env) {
/*
* You could read information from the environment to decide whether
* the user has permission to start a new application
*/
return std::make_unique
});
}
See also
{{Portal|Free and open-source software}}
- Comparison of web-frameworks
- JWt (Java web toolkit), a native Java version of Wt
- Other C++ web frameworks
References
{{Reflist}}
External links
{{Official website}}
{{Web frameworks}}
{{DEFAULTSORT:Wt - Web Toolkit}}
Category:Articles with example C++ code
Category:Rich web application frameworks