Project

General

Profile

Rendering template within a template on the server-side. » start.hpp

Plug Gulp, 03/04/2019 12:27 AM

 
#if !defined(_start_hpp_)
#define _start_hpp_

#include <memory>
#include <sstream>

#include <Wt/WTemplate.h>
#include <Wt/WResource.h>
#include <Wt/Http/Response.h>

class start : public Wt::WResource
{
public:
void handleRequest(Wt::Http::Request const &request, Wt::Http::Response &response) override
{
response.setMimeType("text/html; charset=utf-8");
response.addHeader("Server", "TemplateWidget");

// Main template
Wt::WTemplate t{Wt::WTemplate::tr("start")};

// Inject another template into main template
#define BIND_WIDGET
#if defined(BIND_WIDGET)
// This core dumps because there is no session available
t.bindWidget("w1", std::make_unique<Wt::WTemplate>(Wt::WTemplate::tr("w1")));
#else
// This works
Wt::WTemplate w{Wt::WTemplate::tr("w1")};
std::stringstream ss;
w.renderTemplate(ss);
t.bindString("w1", ss.str());
#endif

// Send it to client
t.renderTemplate(response.out());
}
};

#endif //_start_hpp_

(3-3/3)