Excerpt |
---|
|
How to make your custom component accept and pass on all unknown parameters to the underlying HTML element |
Supporting Informal Parameters
Wiki Markup |
---|
{float:right|background=#eee}
{contentbylabel:title=Related Articles|showLabels=false|showSpace=false|space=@self|labels=parameters}
{float} |
Informal parameters are any additional parameters beyond the parameters explicitly defined for a component using the Parameter annotation.
Div |
---|
style | float:right |
---|
title | Related Articles |
---|
class | aui-label |
---|
|
Content by Label |
---|
showLabels | false |
---|
showSpace | false |
---|
title | Related Articles |
---|
cql | label = "parameters" and space = currentSpace() |
---|
|
|
Any component that closely emulates a particular HTML element should support informal parameters. You'll find that many of the built-in Tapestry components, such as Form, Label and TextField, do exactly that.
...
In the example below we create an Img component, a custom replacement for the <img> tag. Its src parameter will be an asset. We'll use the @SupportsInformalParameters annotation to tell Tapestry that the component should support informal parameters.
Code Block |
---|
|
@SupportsInformalParameters
public class Img
{
@Parameter(required=true, allowNull=false, defaultPrefix=BindingConstants.ASSET)
private Asset src;
@Inject
private ComponentResources resources;
boolean beginRender(MarkupWriter writer)
{
writer.element("img", "src", src);
resources.renderInformalParameters(writer);
writer.end();
return false;
}
}
|
...
Another, equivalent, approach is to use the RenderInformals mixin:
Code Block |
---|
|
public class Img
{
@Parameter(required=true, allowNull=false, defaultPrefix=BindingConstants.ASSET)
private Asset src;
@Mixin
private RenderInformals renderInformals;
void beginRender(MarkupWriter writer)
{
writer.element("img", "src", src);
}
boolean beforeRenderBody(MarkupWriter writer)
{
writer.end();
return false;
}
}
|
...