Template inside template

Added by Marcelo Antunes 2 months ago

I used internalpaths to simulate pages.
I'm using xhtml templates. So each "page" has a menu and his content, the menu is always the same across "pages".
There is some way that i can bind the content and menu, without have to put menu's html across all xhtml templates?

regards
Marcelo


Replies (4)

RE: Template inside template - Added by lm at 2 months ago

I haven't used @WTemplate@s, but I think you specify them as @std::string@s, right? Perhaps you can include the header separately?

std::string template_text {read_from_file("menu.template")};
template_text += read_from_file("homepage.template");
or something like that?

RE: Template inside template - Added by Plug Gulp about 1 month ago

You can create your master template like this:

 <message id="master">
  <html lang="en">
    <head>
    </head>
    <body>
      <div>
        Menu Here
      </div>
      <div>
        ${page_content}
      </div>
    </body>
  </html>
</message>

<message id="page1_content">
  <div>
    Page1 Content
  </div>
</message>

<message id="page2_content">
  <div>
    Page2 Content
  </div>
</message>

Then depending on what page content you want to include you can inject a sub-template for that page content. Here is one way I did it. Please comment out the BIND_WIDGET definition in the source code on that post.

HTH,

~Plug

RE: Template inside template - Added by Marcelo Antunes about 1 month ago

Thanks, I did like this and worked:

auto t = std::make_unique<Wt::WTemplate>(Wt::WString::tr("content"));
//binded every widget of content part here

     auto a = Wt::cpp14::make_unique<Wt::WTemplate>(Wt::WString::tr("menu"));

                std::stringstream ss;
               t->renderTemplate(ss);
                a->bindString("content", ss.str(),Wt::TextFormat::UnsafeXHTML);

//binded every widget of menu part here
    root().addWidget(std::move(a));

Menu template:

<message id="menu">
//i also wrote there html of menu
${content}
</message>

Base template:

<message id="content">
//i also wrote there html of content
</message>

Marcelo

RE: Template inside template - Added by Marcelo Antunes about 1 month ago

Now widgets don't listen events.
That means that if i connect a pushbutton to a slot, when the buttos is clicked, slot isn't called.

Ps: this hapends just on content template

regards
Marcelo

(1-4/4)