Skip to content

Commit

Permalink
Merge pull request #12656 from mkouba/qute-template-instance-data
Browse files Browse the repository at this point in the history
Qute Template - add convenient data() methods
  • Loading branch information
mkouba authored Oct 13, 2020
2 parents 98160e1 + afd442f commit 770db10
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
* <ol>
* <li>Create a new template instance via {@link #instance()} or any convenient method</li>
* <li>Set the model data</li>
* <li>Trigger rendering with {@link TemplateInstance#render()}, {@link TemplateInstance#renderAsync()},
* {@link TemplateInstance#consume(java.util.function.Consumer)} or subscribe to a publisher returned from
* {@link TemplateInstance#publisher()}</li>
* <li>Trigger rendering with {@link TemplateInstance#render()} or any other convenient methods</li>
* </ol>
*/
public interface Template {

/**
* Template instance represents a rendering configuration.
* Create a new template instance to configure the model data.
*
* @return a new template instance
*/
Expand All @@ -44,6 +42,69 @@ default TemplateInstance data(String key, Object data) {
return instance().data(key, data);
}

/**
*
* @param key1
* @param data1
* @param key2
* @param data2
* @return a new template instance
*/
default TemplateInstance data(String key1, Object data1, String key2, Object data2) {
return instance().data(key1, data1).data(key2, data2);
}

/**
*
* @param key1
* @param data1
* @param key2
* @param data2
* @param key3
* @param data3
* @return a new template instance
*/
default TemplateInstance data(String key1, Object data1, String key2, Object data2, String key3, Object data3) {
return instance().data(key1, data1).data(key2, data2).data(key3, data3);
}

/**
*
* @param key1
* @param data1
* @param key2
* @param data2
* @param key3
* @param data3
* @param key4
* @param data4
* @return a new template instance
*/
default TemplateInstance data(String key1, Object data1, String key2, Object data2, String key3, Object data3, String key4,
Object data4) {
return instance().data(key1, data1).data(key2, data2).data(key3, data3).data(key4, data4);
}

/**
*
* @param key1
* @param data1
* @param key2
* @param data2
* @param key3
* @param data3
* @param key4
* @param data4
* @param key5
* @param data5
* @return a new template instance
*
*/
default TemplateInstance data(String key1, Object data1, String key2, Object data2, String key3, Object data3, String key4,
Object data4, String key5, Object data5) {
return instance().data(key1, data1).data(key2, data2).data(key3, data3).data(key4, data4).data(key5, data5);
}

default String render(Object data) {
return data(data).render();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,14 @@ public String map(Object result, Expression expression) {
assertEquals("Not found: foo", expected.getMessage());
}
}

@Test
public void testConvenientDataMethods() {
Engine engine = Engine.builder().addDefaults().build();
assertEquals("1:2", engine.parse("{d1}:{d2}").data("d1", 1, "d2", 2).render());
assertEquals("1:2:3", engine.parse("{d1}:{d2}:{d3}").data("d1", 1, "d2", 2, "d3", 3).render());
assertEquals("1:2:3:4", engine.parse("{d1}:{d2}:{d3}:{d4}").data("d1", 1, "d2", 2, "d3", 3, "d4", 4).render());
assertEquals("1:2:3:4:5",
engine.parse("{d1}:{d2}:{d3}:{d4}:{d5}").data("d1", 1, "d2", 2, "d3", 3, "d4", 4, "d5", 5).render());
}
}

0 comments on commit 770db10

Please sign in to comment.