Project

General

Profile

Is there any WWidget that can act as a container, and contain a pure text node among its children?

Added by Juan Burgos about 9 years ago

Hello guys,

I've been wondering if there is any widget which could translate to html of the following form:

```html

Some Awesome text here

<!-- other widgets -->

```

I know that for text we can use WText, nevertheless WText does not derive from WContainerWidget, therefore it cannot contain other widgets.

There is also WAnchor which does allow to both set text and given that it derives from WContainerWidget, it can also contain other widgets. The problem with WAnchor is that it always surrounds the text with a tag, i.e:

```html



Some Anchor Text

<!-- other widgets -->



```

Is there any other that could be sued for the mentioned purpose?

Thanks in advance,

J.


Replies (9)

RE: Is there any WWidget that can act as a container, and contain a pure text node among its children? - Added by Juan Burgos about 9 years ago

Hi Bruce, thanks for your answer.

Sadly WTemplate also does not inherit from WContainerWidget, therefore it cannot contain other widgets. The only choice is to 'bind' the child widgets manually to the template. The problem is that if the widget tree goes down by many levels, then the task becomes very tedious.

Best,

J.

RE: Is there any WWidget that can act as a container, and contain a pure text node among its children? - Added by Wim Dumon about 9 years ago

bindWidget() of a WTemplate will reparent the widget to the WTemplate, thus it will be deleted when the template to which it is bound is deleted.

I will modify the documentation of WTemplate to make this more explicit; from resolveWidget() and bindWidget() you can kind of deduce that this is the case, but it's better to mention this explicitly.

Wim.

RE: Is there any WWidget that can act as a container, and contain a pure text node among its children? - Added by Juan Burgos about 9 years ago

Hi Wim,

That is a nice feature indeed, thanks for the info. Is there a way to add children to a WTemplate without using the bindWidget() interface? I.e. something in the lines of:

```cpp

WTemplate *templ = new WTemplate();

templ->setTemplateText("Some awesome text", TextFormat::PlainText );

// option 1

Wt::WContainerWidget *container1 = new Wt::WContainerWidget( templ );

// option 2

Wt::WContainerWidget *container2 = new Wt::WContainerWidget();

templ->addWidget(container2);

```

Best, J.

RE: Is there any WWidget that can act as a container, and contain a pure text node among its children? - Added by Wim Dumon about 9 years ago

To what purpose? The only way to display a container in a template is through bindWidget... You can bind a WContainerWidget to a WTemplate and then add widgets to the WContainerWidget, if that is what you are looking for?

Wim.

RE: Is there any WWidget that can act as a container, and contain a pure text node among its children? - Added by Juan Burgos about 9 years ago

I am working on a new feature for the WtDesigner tool. The feature is to map an existing HTML file as close as possible into Wt C code. Most of HTML constructs are possible to map using wxisting WWdigets, the only one I haven't been able to map is HTML code of the form:

```html

Some Awesome text here

<!-- other widgets -->

```

It seems the only way to get code of that form is to use WTemplate and binding all the children widgets recursivelly. This is a problem, because C code generation in WtDesigner uses the WWidget tree interface of the form:

```cpp

// option 1

Wt::WContainerWidget *container1 = new Wt::WContainerWidget( new_widget);

// option 2

Wt::WContainerWidget *container2 = new Wt::WContainerWidget();

templ->addWidget( new_widget );

```

So I though maybe you guys knew of an alternative on how to achieve that. But maybe is not possible, I don't know.

J.

RE: Is there any WWidget that can act as a container, and contain a pure text node among its children? - Added by Wim Dumon about 9 years ago

Hmm yes I think you will have to use bind in this case:

<div id="div1" class="container">
Some Awesome text here
<div id="div2" class="container">
${placeholder}
</div>
</div>

and then

template->bind("placeholder", widget)

I'm not sure if I understand your 'recursively' remark.

If I understand your intention correctly, you could go through the html page, find all interactive componentents, and replace them with placeholders to create the template. Then in C generate the code to bind the interactive components to the WTemplate class. Did I miss the recursion?

Wim.

RE: Is there any WWidget that can act as a container, and contain a pure text node among its children? - Added by Juan Burgos about 9 years ago

Hi Wim,

Thank you for the answer. What I mean with recursively, is that, my end goal is to map the complete DOM to C objects as close as possible.

If I have that text special case nested into another text special case, then I need to map the outer case to a WTemplate with an outer placeholder, and then bind to that an inner WTemplate and bind whatever comes next down-stream in the DOM. For example the case:

<div id="div1" class="container">
    Some Awesome outer text here
    <div id="div2" class="container">
        Some Awesomer inner text here
        <input id="input1"></input>
        <button id="button1"></button>
        <!-- other inner widgets -->
    </div>
    <!-- other outer widgets -->
</div>

See, I wanted to make a 1 by 1 mapping as close as possible, but it seems is too much hassle. I haven't found a way in Wt to just add a text node, without it being surrounded by some undesired html tags.

I think I will go for the WAnchor and hope the enforced tag around the text does not mess up the original design.

Thank you so far for the help,

J.

RE: Is there any WWidget that can act as a container, and contain a pure text node among its children? - Added by Wim Dumon about 9 years ago

Juan,

Not sure why you refer to WAnchor... Did you know that an inline WText will be rendered as a span (setInline(false))? That is maybe less awkward than a WAnchor?

Wim.

    (1-9/9)