Multiple Markups Per Page

Variations

Each Wicket component has at least one associated markup (in the form of an HTML file). It is possible to have multiple markup alternatives for one component. These are called "variations", and have an HTML file with a name like "ComponentName_foo.html". Which markup variation is used, is determined by the getVariation() method of Component, i.e. if it returns the string "foo", the HTML file "ComponentName_foo.html" is loaded instead of "ComponentName.html".

Variations are inherited down the component hierarchy, and the default is null, which means no variation (i.e. load "ComponentName.html"). Any component can override the getVariation() method to change this behaviour.

If a markup variation with the name returned by getVariation() is not found, Wicket will load the default markup for that component.

The example below shows how you can use a request parameter to load different markup variations for a Page on the fly.

Example

With wicket, every html markup page is controlled by a Class file, a panel, etc. It does make sense at times to have a single class file manage multiple markup files, or "variations" in Wicket land. You can do so using the following code in the Page:

    public MyPage(PageParameters parameters) {

        if(parameters!=null) {
            this.variant = (String)parameters.get("variant");
        }
    }

    private String variant;

    public String getVariation() {
        return variant;
    }

Where variant is passed in as a request parameter, so a request to /MyPage?variant=abc would retrieve MyPage_abc.html and /MyPage?variant=xyz would retrieve MyPage_xyz.html.

  • No labels