Fragment has been edited by Martijn Dashorst (Apr 09, 2007).

(View changes)

Content:

Fragments are effectively in-line Panels, i.e. they provide a way to maintain a panel's piece of markup within the parent's markup file.

They're just there to help with the cases where you don't need to share the panel contents between different pages but are just using them on a single page. In other words, they're a convinience feature for people to embed repeating but non-reusable pieces of markup into the parent's markup file.

Example

e.g. You have a target myPanel

<span wicket:id="myPanel">Example input (will be removed)</span>

and in the same markup file you have some fragments, i.e.

<wicket:fragment wicket:id="frag1">panel 1</wicket:fragment>
<wicket:fragment wicket:id="frag2">panel 2</wicket:fragment>

Then at run-time, in your page code, you can decide which to use, e.g.

add(new Fragment("myPanel", "frag1");

which will result in the markup from frag 1 being rendered, i.e. panel 1


Note

If the component that you are putting the fragment in is a subclass, the fragment's markup must be inside the <wicket:extend></wicket:extend> tag:

<wicket:extend>
    ...
    <span wicket:id="myPanel">Example input (will be removed)</span>
    ...
    <wicket:fragment wicket:id="frag1">panel 1</wicket:fragment>
    <wicket:fragment wicket:id="frag2">panel 2</wicket:fragment>
</wicket:extend>

Fragments from other pages

It's possible to include fragments from other pages as well, even without using the <wicket:fragment>...</wicket:fragment> tags:

NB. This is not recommended usage: consider using Panels instead

Test.html
<html>
<head>
	<title>Title</title>
</head>
<body>
	<span wicket:id="target">
		This will be replaced
	</span>
</body>
</html>
Test.java
public class Test extends WebPage {

	public Test() {
		add(new Snippets().createTable("target"));
	}
}
Snippets.html
<html>
<head>
	<title>Snippets</title>
</head>
<body>
	<span wicket:id="fragment.source">
		<table>
			<tr>
				<td>Hello</td>
				<td>World</td>
			</tr>
		</table>
	</span>
</body>
</html>
Snippets.java
public class Snippets extends WebPage {

	private final WebMarkupContainer fragmentSource;

	public Snippets() {
		add(fragmentSource = new WebMarkupContainer("fragment.source"));
		setMarkupStream(getAssociatedMarkupStream(true));
	}

	public Component createTable(String id) {
		return new Fragment(id, fragmentSource.getId(), this);
	}
}

will be rendered as (ignoring some irrelevant _javascript_ code in the header):

rendered output
<html>
<head>
	<title>Title</title>
</head>
<body>
	<span>
		<table border="1">
			<tr>
				<td>Hello</td>
				<td>World</td>
			</tr>
		</table>
	</span>
</body>
</html>

Reply via email to