I'm using markup inheritance in Wicket 6.14.0 to create a simple widget with title and content. The parent's markup looks like this:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket>
<wicket:panel>
<div wicket:id="widgetBox">
<div wicket:id="widgetTitle"><wicket:enclosure child="icon"><span class="icon"><i wicket:id="icon"></i></span></wicket:enclosure>
<h5 wicket:id="headline"></h5>
</div>
<div wicket:id="widgetContent"><wicket:child/></div>
</div>
</wicket:panel>
</html>
Its code is pretty straight-forward, but for the sake of completeness:
public class ParentPanel extends Panel {
public ParentPanel(final String id, final IModel<String> title) {
super(id, title);
WebMarkupContainer widgetBox = new WebMarkupContainer("widgetBox");
add(widgetBox);
final WebMarkupContainer widgetTitle = new WebMarkupContainer("widgetTitle");
widgetBox.add(widgetTitle);
final WebMarkupContainer icon = new WebMarkupContainer("icon");
if (titleIcon != null) {
// custom class
icon.add(new IconBehavior("titleIcon"));
} else {
icon.setVisible(false);
}
widgetTitle.add(icon);
widgetTitle.add(new Label("headline", title));
widgetContent = new WebMarkupContainer("widgetContent");
widgetContent.add(new WidgetContentBehavior());
widgetBox.add(widgetContent);
}
}
Now for the child component. Here's its most simple markup:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket>
<wicket:extend>
<span wicket:id="foobar"></span>
</wicket:extend>
</html>
And of course the Java code:
public class ChildPanel extends ParentPanel {
public ChildPanel(final String id, final IModel<String> title) {
super(id, title);
add(new Label("foobar", "Foobar"));
}
}
What I'm expecting of course is a rendered page saying "Foobar" where the wicket:child tag is placed. Instead there's this RuntimeException:
Last cause: Unable to find component with id 'foobar' in [TransparentWebMarkupContainer [Component id = wicket_extend3]]
Expected: 'foobar:widgetBox:widgetContent:wicket_child2:wicket_extend3:foobar'.
Found with similar names: 'foobar', foobar:foobar'
It seems as if markup inheritance does not support wicket:child tags within a MarkupContainer and therefore messes up the hierarchy. I also considered using a Border instead, getting the same outcome though.
My question of course is how to get this work by maintaining the current hierarchy, as it's important to have control (read: a Java Object) of widgetBox and widgetContent.
Here's another question regarding your issue: Can a wicket:child tag be nested under another Component on the Page?
The answers provided use TransparentWebMarkupContainer
to solve the issue.
However, as I found out, note that using TransparentWebMarkupContainer
will fail if your child page defines wicket:fragment
s. See issues WICKET-4545 and WICKET-5440