CrazySabbath
CrazySabbath

Reputation: 1334

Dynamically increment SafeHtmlTemplates variable value during runtime

Is it possible to increment (or change in general) added value to interface during run time? For example, integer is used as a variable, would it be possible to increment the variable after it was added?

Let's say I have SafeHtmlTemplates interface:

public interface MyTemplate extends SafeHtmlTemplates {
    @Template("<div id=\"{0}\"></div>") 
    SafeHtml temp(Integer id);
}

If I were to use it:

MyTemplate tpls = GWT.create(MyTemplate.class);

for (int i = 0; i < 2; i++) {
    tpls.temp(i);
}

I would get:

<div id="0"></div>
<div id="1"></div>

However I would like to get:

<div id="1"></div>
<div id="2"></div>

By incrementing {0} after it's added somehow (something like {0} + {offsetVariable}, so far I think that's not possible (or am just unable to figure out how to do that).

Any ideas?

Upvotes: 0

Views: 79

Answers (1)

Adam
Adam

Reputation: 5599

You can use a helper class like this:

public class MyTemplateHelper implements MyTemplate {

    private MyTemplate tpls = GWT.create(MyTemplate.class);

    @Override
    public SafeHtml temp(Integer id) {
        return tpls.temp(id + 1);
    }
}

Please, notice that MyTemplateHelper can implement MyTemplate.

Compare usage:

MyTemplate tpls;

tpls = GWT.create(MyTemplate.class);    // original template class
for(int i = 0; i < 2; i++)
    tpls.temp(i);

//  you'll get:
//  <div id="0"></div>
//  <div id="1"></div>

tpls = new MyTemplateHelper();          // helper class
for(int i = 0; i < 2; i++)
    tpls.temp(i);

//  you'll get:
//  <div id="1"></div>
//  <div id="2"></div>

Upvotes: 2

Related Questions