Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating commented-out content with Wicket

For debug reasons, and on a whim, I'd like to include certain information in a Wicket page's HTML output that is enclosed in HTML comments.

The output would be something like...

<!-- 
<div wicket:id="foo"> 1234 </div>
-->

...where "1234" is some interesting, dynamically created piece of information.

I have tried, to no avail:

  • <!-- <div wicket:id="foo"></div> --> → Wicket complains that element with id "foo" is missing from the HTML page
  • enclose in <wicket:remove> → such sections cannot contain elements with wicket:id
  • label.setVisible(false) → Wicket doesn't output the label at all
  • new Label("foo", "<!-- " + foo + " -->") → the < and > get escaped

So, can you do this with Wicket (easily), or should I just forget about it?

like image 861
Jonik Avatar asked Oct 14 '10 13:10

Jonik


3 Answers

How about this?

class CommentOutModifier extends AbstractBehavior {
    private static final long serialVersionUID = 1L;

    @Override
    public void beforeRender(Component component) {
        component.getResponse().write("<!--");
    }

    @Override
    public void onRendered(Component component) {
      component.getResponse().write("-->");
    }
}

add(new Label("tohide", "Hi, can you see me?").add(new CommentOutModifier()));

then, putting:

<span wicket:id="tohide"></span>

in your markup will yield:

<!--<span>Hi, can you see me?</span>-->
like image 177
Eelco Avatar answered Nov 18 '22 07:11

Eelco


Label l = new Label("foo", "<!-- " + foo + " -->");
l.setEscapeModelStrings(false);

Its not pretty but it's quick and easy. I also believe there is a specific wicket setting (somewhere in application) which which you can turn on to prevent it from stripping comments, but I honestly can't remember where I saw it.

Edit: Added comment worker

Edit2: Implemented Eelco's behaviour for completeness. Its better than my approach anyway.

public enum Comment {
;
    /**
     * creates a wicket comment (extends label
     */
    public static Label newComment(String id, String comment) {
        Label label = new Label(id, comment);
        label.add(commentBehaviour());
        return label;
    }

    /**
     * Adds &lt;!-- and --&gt around the component.
     */
    public static AbstractBehavior commentBehaviour() {
        return new AbstractBehavior() {
            private static final long serialVersionUID = 1L;

            @Override
            public void beforeRender(Component component) {
                component.getResponse().write("<!--");
            }

            @Override
            public void onRendered(Component component) {
                component.getResponse().write("--!>");
            }
        };
    }
}

add(Comment.newComment("comment","Something worth commenting upon"));
like image 27
BjornS Avatar answered Nov 18 '22 07:11

BjornS


Played around a little and got to this:

Label label = new Label("commented", "Content") {
    @Override
    protected void onComponentTag(ComponentTag tag) {
        tag.setName("!--");
        super.onComponentTag(tag);
    }
};
add(label);

But it's not pretty..: <span wicket:id="commented">This will be replaced</span>
becomes: <!-- wicket:id="commented">Content</!-->

But at least it won't interfere with layout / css styles.

like image 1
Tim Avatar answered Nov 18 '22 06:11

Tim