Basic Wt Example including WAnchors
Added by Andres Jaimes over 12 years ago
Hello everyone,
I have the following code. It works as it should. It took me a while to understand how to create a link, but everything is working. I want to ask you for your critics. I want to see if I understand the way I have to use Wt. This example shows some code to add some web parts like a header, footer, sidebar (I know about layouts, however I'm still figuring things out) and a link to a couple pages: home and page1.
This is a very simplistic version of an app. In a real application I would separate in different files (cpp's and h's), etc... however I'm trying to make an easy example too see how it works.
Thank you so much.
#include <Wt/WAnchor>
#include <Wt/WApplication>
#include <Wt/WContainerwidget>
#include <Wt/WLink>
#include <Wt/WText>
using namespace Wt;
class ControlExample: public WApplication {
private:
std::string appName;
WContainerWidget* _content;
public:
ControlExample(const WEnvironment &env): WApplication(env) {
appName = "Application Name";
setTitle(appName);
_content = 0;
internalPathChanged().connect(this, &ControlExample::onInternalPathChange);
header();
home();
sidebar();
footer();
}
WContainerWidget* content() {
if (_content == 0) {
_content = new WContainerWidget(root());
_content->setId("content");
}
return _content;
}
void onInternalPathChange() {
content()->clear();
if (internalPath() == "/") {
home();
}
else if (internalPath() == "/page1") {
page1();
}
}
void header() {
WContainerWidget* header = new WContainerWidget(root());
header->setId("header");
header->addWidget(new WText("<h1>" + appName + "</h1>"));
}
void sidebar() {
WContainerWidget* sidebar = new WContainerWidget(root());
sidebar->setId("sidebar");
sidebar->addWidget(new WText("Sidebar Information"));
}
void footer() {
WContainerWidget* footer = new WContainerWidget(root());
footer->setId("footer");
footer->addWidget(new WText("Developed using C++/Wt"));
}
void home() {
content()->addWidget(new WText("<strong>Home</strong> content and a link to "));
WAnchor* a = new WAnchor(WLink(WLink::InternalPath, "/page1"), "page 1", content());
}
void page1() {
content()->addWidget(new WText("<strong>Page 1</strong> content and a link to "));
WAnchor* a = new WAnchor(WLink(WLink::InternalPath, "/"), "home", content());
}
};
WApplication* createApplication(const WEnvironment &env) {
return new ControlExample(env);
}
int main(int argc, char** argv) {
return WRun(argc, argv, &createApplication);
}
Replies (3)
RE: Basic Wt Example including WAnchors - Added by Wim Dumon over 12 years ago
Hello,
In this case, I would not use WAnchor, put everything in a WText, and use WText::setInternalPathEncoding(true) to let Wt fixup anchors in the text which refer to internal paths.
Thus:
WText *t = new WText("<strong>Page 1</strong> content and a link to <a href='#/page1'>page1</a>"));
t->setInternalPathEncoding(true);
BR,
Wim.
RE: Basic Wt Example including WAnchors - Added by Andres Jaimes over 12 years ago
Thanks Wim, that is an excellent tip. And I'm glad to see that is your only comment... I think it means that I'm not so wrong about how to use it...
Thanks again
RE: Basic Wt Example including WAnchors - Added by Wim Dumon over 12 years ago
Hello Andres,
Good that you mention that, because there's an other important remark: avoid the use of setId(). Wt will auto-generate the id, to ensure that it is unique within a webpage. If you use setId() yourself, your widget cannot be instantiated more than once at any given time, and it is up to you to avoid that other classes (widgets) have the same id as the one you specify.
BR,
Wim.