From afd442f2e258a54c19634beddc9e7b6eed8ca78d Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Mon, 12 Oct 2020 14:04:11 +0200 Subject: [PATCH] Qute Template - add convenient data() methods --- .../main/java/io/quarkus/qute/Template.java | 69 +++++++++++++++++-- .../test/java/io/quarkus/qute/SimpleTest.java | 10 +++ 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/Template.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/Template.java index 84adbb57157c2..2de0e141f899c 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/Template.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/Template.java @@ -10,15 +10,13 @@ *
    *
  1. Create a new template instance via {@link #instance()} or any convenient method
  2. *
  3. Set the model data
  4. - *
  5. 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()}
  6. + *
  7. Trigger rendering with {@link TemplateInstance#render()} or any other convenient methods
  8. *
*/ public interface Template { /** - * Template instance represents a rendering configuration. + * Create a new template instance to configure the model data. * * @return a new template instance */ @@ -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(); } diff --git a/independent-projects/qute/core/src/test/java/io/quarkus/qute/SimpleTest.java b/independent-projects/qute/core/src/test/java/io/quarkus/qute/SimpleTest.java index eda1135e43e5d..02d4658868bba 100644 --- a/independent-projects/qute/core/src/test/java/io/quarkus/qute/SimpleTest.java +++ b/independent-projects/qute/core/src/test/java/io/quarkus/qute/SimpleTest.java @@ -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()); + } }