You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Marcel Barbosa Pinto <ma...@gmail.com> on 2017/01/13 13:42:54 UTC
Idea for a template component
Hi guys,
I had an idea last night and decided to implement it.
On my wicket app I have a lot of labels that is used just to display the
model values to the user.
So my code was some thing like this:
WebMarkupContainer container = new WebMarkupContainer("container", new
CompoundPropertyModel(myModel));
container.add(
new Label("prop1");
new Label("prop2.name");
new Label("prop2.lastname")
);
add(container);
html was like this:
<div wicket:id="container">
Prop1: <span wicket:id="prop1"></span>
Prop2 name: <span wicket:id="prop2.name"></span>
Prop2 lastname: <span wicket:id="prop2.lastname"></span>
</div>
So I decided to try to simplify the HTML code and created a
TemplateMarkupComponent which basically uses the VariableInterpolator class
and PropertyResolver:
java looks like this:
TemplateMarkupContainer container = new
TemplateMarkupContainer("container", myModel);
add(container);
Html code:
<div wicket:id="container">
Prop1: ${prop1}
Prop2 name: ${prop2.name}
Prop2 lastname: ${prop2.lastname}
</div>
To display the values the component apply the PropertyConverter, so fields
are displayed with the correct format.
It uses the same expressions that PropertyModel uses to get its properties.
I think this could be used to render scripts as well.
You can just remove the expression tag from HTML without having to change
the java code.
You can choose if and Exception should be raised if the Model doesn't
contains the property, like the MapVariableInterpolator does.
These two I am still thinking about:
You can override the onValue(String expression, Object value) in order to
modify the value that is rendered
If you pass a Model with is not an Object with properties, like just a
String or Int, you can render it by using ${this}
It is just two classes:
ModelVariableInterpolator.java
TemplateMarkupContainer.java
Do you think that this could be useful?
What problems could be raised by this approach?
--
Marcel Barbosa Pinto
Re: Idea for a template component
Posted by Marcel Barbosa Pinto <ma...@gmail.com>.
Hi Martin,
I made this quickstart for testing the possibilities:
https://github.com/mbppower/wicketTemplateMarkupComponent
The implementation classes are located at
https://github.com/mbppower/wicketTemplateMarkupComponent/tree/master/src/main/java/org/apache/wicket/markup/html
I think this is indeed really simple, but would be nice to have it
available, since it requires some knowledge that may not be clear for new
users.
It's up to you man, just wanted to show this ;)
On Mon, Jan 16, 2017 at 5:48 AM, Martin Grigorov <mg...@apache.org>
wrote:
> Hi Marcel,
>
> Thank you for your suggestion but I think it doesn't worth it to be part of
> wicket-core.
> It is as simple as
>
> @Override public void onComponentTagBody(final MarkupStream markupStream,
> final ComponentTag openTag) {
> replaceComponentTagBody(markupStream, tag, interpolate(body));
> }
>
> Same could be achieved with CompoundPropertyModel (+ component queueing if
> the hierachy should be changed frequently).
>
>
> Martin Grigorov
> Wicket Training and Consulting
> https://twitter.com/mtgrigorov
>
> On Fri, Jan 13, 2017 at 2:42 PM, Marcel Barbosa Pinto <
> marcel.power@gmail.com> wrote:
>
> > Hi guys,
> >
> > I had an idea last night and decided to implement it.
> >
> > On my wicket app I have a lot of labels that is used just to display the
> > model values to the user.
> > So my code was some thing like this:
> >
> > WebMarkupContainer container = new WebMarkupContainer("container", new
> > CompoundPropertyModel(myModel));
> > container.add(
> > new Label("prop1");
> > new Label("prop2.name");
> > new Label("prop2.lastname")
> > );
> > add(container);
> >
> > html was like this:
> > <div wicket:id="container">
> > Prop1: <span wicket:id="prop1"></span>
> > Prop2 name: <span wicket:id="prop2.name"></span>
> > Prop2 lastname: <span wicket:id="prop2.lastname"></span>
> > </div>
> >
> > So I decided to try to simplify the HTML code and created a
> > TemplateMarkupComponent which basically uses the VariableInterpolator
> class
> > and PropertyResolver:
> >
> > java looks like this:
> >
> > TemplateMarkupContainer container = new
> > TemplateMarkupContainer("container", myModel);
> > add(container);
> >
> > Html code:
> >
> > <div wicket:id="container">
> > Prop1: ${prop1}
> > Prop2 name: ${prop2.name}
> > Prop2 lastname: ${prop2.lastname}
> > </div>
> >
> > To display the values the component apply the PropertyConverter, so
> fields
> > are displayed with the correct format.
> > It uses the same expressions that PropertyModel uses to get its
> properties.
> > I think this could be used to render scripts as well.
> >
> > You can just remove the expression tag from HTML without having to change
> > the java code.
> > You can choose if and Exception should be raised if the Model doesn't
> > contains the property, like the MapVariableInterpolator does.
> >
> > These two I am still thinking about:
> > You can override the onValue(String expression, Object value) in order to
> > modify the value that is rendered
> > If you pass a Model with is not an Object with properties, like just a
> > String or Int, you can render it by using ${this}
> >
> > It is just two classes:
> > ModelVariableInterpolator.java
> > TemplateMarkupContainer.java
> >
> >
> > Do you think that this could be useful?
> > What problems could be raised by this approach?
> >
> >
> > --
> >
> > Marcel Barbosa Pinto
> >
>
--
Marcel Barbosa Pinto
55 11 98255 8288
Re: Idea for a template component
Posted by Martin Grigorov <mg...@apache.org>.
Hi Marcel,
Thank you for your suggestion but I think it doesn't worth it to be part of
wicket-core.
It is as simple as
@Override public void onComponentTagBody(final MarkupStream markupStream,
final ComponentTag openTag) {
replaceComponentTagBody(markupStream, tag, interpolate(body));
}
Same could be achieved with CompoundPropertyModel (+ component queueing if
the hierachy should be changed frequently).
Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov
On Fri, Jan 13, 2017 at 2:42 PM, Marcel Barbosa Pinto <
marcel.power@gmail.com> wrote:
> Hi guys,
>
> I had an idea last night and decided to implement it.
>
> On my wicket app I have a lot of labels that is used just to display the
> model values to the user.
> So my code was some thing like this:
>
> WebMarkupContainer container = new WebMarkupContainer("container", new
> CompoundPropertyModel(myModel));
> container.add(
> new Label("prop1");
> new Label("prop2.name");
> new Label("prop2.lastname")
> );
> add(container);
>
> html was like this:
> <div wicket:id="container">
> Prop1: <span wicket:id="prop1"></span>
> Prop2 name: <span wicket:id="prop2.name"></span>
> Prop2 lastname: <span wicket:id="prop2.lastname"></span>
> </div>
>
> So I decided to try to simplify the HTML code and created a
> TemplateMarkupComponent which basically uses the VariableInterpolator class
> and PropertyResolver:
>
> java looks like this:
>
> TemplateMarkupContainer container = new
> TemplateMarkupContainer("container", myModel);
> add(container);
>
> Html code:
>
> <div wicket:id="container">
> Prop1: ${prop1}
> Prop2 name: ${prop2.name}
> Prop2 lastname: ${prop2.lastname}
> </div>
>
> To display the values the component apply the PropertyConverter, so fields
> are displayed with the correct format.
> It uses the same expressions that PropertyModel uses to get its properties.
> I think this could be used to render scripts as well.
>
> You can just remove the expression tag from HTML without having to change
> the java code.
> You can choose if and Exception should be raised if the Model doesn't
> contains the property, like the MapVariableInterpolator does.
>
> These two I am still thinking about:
> You can override the onValue(String expression, Object value) in order to
> modify the value that is rendered
> If you pass a Model with is not an Object with properties, like just a
> String or Int, you can render it by using ${this}
>
> It is just two classes:
> ModelVariableInterpolator.java
> TemplateMarkupContainer.java
>
>
> Do you think that this could be useful?
> What problems could be raised by this approach?
>
>
> --
>
> Marcel Barbosa Pinto
>