You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Carlos Pita <ca...@gmail.com> on 2007/08/18 22:40:46 UTC

Template tags

Hi all,
one need that repeatedly araises in our project is the one to write some
javascript support code to be called from event behaviors or to integrate
external libraries. Often these code fragments need to reference other
wicket managed tags that have their own auto generated ids. So normally we
divide the code a bit unnaturally in a way that code with id references is
programatically generated and the rest, static one, resides at the markup
file. IMO a nifty solution to keep all the code in one place could be to
write a component that simply treats its content as a template and replaces
placeholders with values passed as a model (say a Map) to its constructor.
So the template can be a snippet of javascript, and the model just the
mapping id -> target.getMarkupId(). The same idea could be extended to a
template system as velocity or freemarker that does more than placeholder
filling, but I think that would be way too much. What I would like to know
is if someone has faced a similar concern, what solution did he find,
whether or not there is provided component with the proposed behavior
(although it would be trivial to implement a home brew one), and your
opinions in general.
Thank you in advance
Regards,
Carlos

Re: Template tags

Posted by Carlos Pita <ca...@gmail.com>.
Yes more or less like that. But I was thinking in terms of onRender or
onComponentTagBody because I didn't know of the transformer behavior, which
looks pretty elegant. And I would also add a couple of constructors that
take one or two mappings parameters as the model to make the common case a
oneliner (I think they do that with spring mvc models). Thanks for the
example.

On 8/18/07, Igor Vaynberg <ig...@gmail.com> wrote:
>
> you mean something like
>
> class Template extends WebMarkupContainer {
> public Template(String id, IModel model) {
>     super(id, model);
>     add(new AbstractTransformerBehavior() {
>        public CharSequence transform(Component c, CharSequence output) {
>           return new
> TextTemplate(output).asString((Map)Template.this.getModelObject());
>        }
>      });
>   }
> }
>
> -igor
>
>
> On 8/18/07, Carlos Pita <ca...@gmail.com> wrote:
> >
> > Hi all,
> > one need that repeatedly araises in our project is the one to write some
> > javascript support code to be called from event behaviors or to
> integrate
> > external libraries. Often these code fragments need to reference other
> > wicket managed tags that have their own auto generated ids. So normally
> we
> > divide the code a bit unnaturally in a way that code with id references
> is
> > programatically generated and the rest, static one, resides at the
> markup
> > file. IMO a nifty solution to keep all the code in one place could be to
> > write a component that simply treats its content as a template and
> > replaces
> > placeholders with values passed as a model (say a Map) to its
> constructor.
> > So the template can be a snippet of javascript, and the model just the
> > mapping id -> target.getMarkupId(). The same idea could be extended to a
> > template system as velocity or freemarker that does more than
> placeholder
> > filling, but I think that would be way too much. What I would like to
> know
> > is if someone has faced a similar concern, what solution did he find,
> > whether or not there is provided component with the proposed behavior
> > (although it would be trivial to implement a home brew one), and your
> > opinions in general.
> > Thank you in advance
> > Regards,
> > Carlos
> >
>

Re: Template tags

Posted by Igor Vaynberg <ig...@gmail.com>.
you mean something like

class Template extends WebMarkupContainer {
 public Template(String id, IModel model) {
    super(id, model);
    add(new AbstractTransformerBehavior() {
       public CharSequence transform(Component c, CharSequence output) {
          return new
TextTemplate(output).asString((Map)Template.this.getModelObject());
       }
     });
  }
}

-igor


On 8/18/07, Carlos Pita <ca...@gmail.com> wrote:
>
> Hi all,
> one need that repeatedly araises in our project is the one to write some
> javascript support code to be called from event behaviors or to integrate
> external libraries. Often these code fragments need to reference other
> wicket managed tags that have their own auto generated ids. So normally we
> divide the code a bit unnaturally in a way that code with id references is
> programatically generated and the rest, static one, resides at the markup
> file. IMO a nifty solution to keep all the code in one place could be to
> write a component that simply treats its content as a template and
> replaces
> placeholders with values passed as a model (say a Map) to its constructor.
> So the template can be a snippet of javascript, and the model just the
> mapping id -> target.getMarkupId(). The same idea could be extended to a
> template system as velocity or freemarker that does more than placeholder
> filling, but I think that would be way too much. What I would like to know
> is if someone has faced a similar concern, what solution did he find,
> whether or not there is provided component with the proposed behavior
> (although it would be trivial to implement a home brew one), and your
> opinions in general.
> Thank you in advance
> Regards,
> Carlos
>