Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixed bad links due to copy-paste from cwiki-test

Scrollbar

...

 

Excerpt
hiddentrue

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 additional parameters beyond the formal parameters defined for a component using the Parameter annotation.

Informal parameters are any additional parameters (aka HTML attributes) beyond the those explicitly defined for a component using the @Parameter annotation.

Div
stylefloat:right; margin: 1em
titleRelated Articles
classaui-label
Content by Label
showLabelsfalse
showSpacefalse
titleRelated Articles
cqllabel = "parameters" and space = currentSpace()

Any A component that closely emulates a particular HTML element should also support informal parameters, because it gives users of your component the ability to easily add HTML attributes to the HTML that your component emits. You'll find that many most of the built-in Tapestry components, such as Form, Label and TextField, do exactly that.

Normally, specifying additional parameters for a component, beyond its formal parameters, does nothing: the additional parameters are ignored.

The SupportsInformalParameters annotation is used to identify a component for which informal parameters are to be kept.

To support informal parameters, a component class should use either the @SupportsInformalParameters annotation or the RenderInformals mixin. Otherwise, providing informal parameters to a component will do nothing: any additional parameters will be ignored.

Approach 1: @SupportsInformalParameters

In the example below we create The example is 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
languagejava
titleImg.java

@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;
    }
}

The call to renderInformalParameters() is what converts and outputs the informal parameters. It should occur after your code has rendered attributes into the element (earlier written attributes will not be overwritten by later written attributes).

Returning false from beginRender() ensures that the body of the component is not rendered, which makes sense for a an <img> tag, which has no body.

Approach 2: RenderInformals

Another, equivalent, approach Another option is to use the RenderInformals mixin (:

Code Block
languagejava
titleImg.java

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;
    }
}

This variation splits the rendering of the tag in two pieces, so that the RenderInformals mixin can operate (after beginRender() and before beforeRenderBody()).

Approach 3: Extend the "Any" component

Another approach is to have your component class extend Tapestry's Any component, which already supports informal parameters:

Code Block
languagejava
titleImg.java
public class Img extends Any { ... }