From 363958484eb93a21d38d14769440b4ccead92100 Mon Sep 17 00:00:00 2001 From: "yinan.liu" Date: Tue, 15 Nov 2022 09:09:46 +0100 Subject: [PATCH] Add inline template to TemplateEngine With this commit, TemplateEngine is able to initialize a Template instance from a template string without having to creating a template file. Upgrade github checkout action version. --- .github/workflows/build.yml | 9 +++++---- .../workflows/gradle-wrapper-validation.yml | 2 +- .github/workflows/release.yml | 9 +++++---- docs/message.md | 19 +++++++++++++++++++ symphony-bdk-bom/build.gradle | 4 ++-- .../bdk/examples/TemplateExampleMain.java | 11 +++++++++++ 6 files changed, 43 insertions(+), 11 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7224868d8..72821a2c3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,13 +16,14 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up JDK 1.8 - uses: actions/setup-java@v1 + uses: actions/setup-java@v3 with: - java-version: 1.8 + distribution: 'adopt' + java-version: '8' - name: Cache Gradle packages - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: | ~/.gradle/caches diff --git a/.github/workflows/gradle-wrapper-validation.yml b/.github/workflows/gradle-wrapper-validation.yml index 6ac96ed9d..65e060aa4 100644 --- a/.github/workflows/gradle-wrapper-validation.yml +++ b/.github/workflows/gradle-wrapper-validation.yml @@ -8,5 +8,5 @@ jobs: name: "Validation" runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: gradle/wrapper-validation-action@v1 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0b29987d9..dc7734b08 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,13 +14,14 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up JDK 1.8 - uses: actions/setup-java@v1 + uses: actions/setup-java@v3 with: - java-version: 1.8 + distribution: 'adopt' + java-version: '8' - name: Cache Gradle packages - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: | ~/.gradle/caches diff --git a/docs/message.md b/docs/message.md index 9ca8c25e2..fa540c428 100644 --- a/docs/message.md +++ b/docs/message.md @@ -49,6 +49,8 @@ engine implementations: > In the code examples below, we will assume that FreeMarker as been selected as template engine implementation. > See [how to select the template engine implementation](#select-your-template-engine-implementation). +#### Template file + First you need to define your message template file. Here `src/main/resources/templates/simple.ftl`: ``` Hello, ${name}! @@ -125,5 +127,22 @@ dependencies { > :warning: If multiple implementations found in classpath, an exception is throw in order to help you to define which one > your project really needs to use. + +#### Inline template string + +Simple template can be created as an inline template. Take the same example above, simply pass the template string to the function, such like + +```java +public class Example { + + public static void main(String[] args) { + final SymphonyBdk bdk = new SymphonyBdk(loadFromClasspath("/config.yaml")); + final Template template = bdk.messages().templates().newTemplateFromString("Hello, ${name}!"); + final String content = template.process(Collections.singletonMap("name", "Freemarker")); + log.info(content); + } +} +``` + ---- [Home :house:](./index.md) diff --git a/symphony-bdk-bom/build.gradle b/symphony-bdk-bom/build.gradle index 7483c9891..f5b0f420c 100644 --- a/symphony-bdk-bom/build.gradle +++ b/symphony-bdk-bom/build.gradle @@ -16,9 +16,9 @@ repositories { dependencies { // import Spring Boot's BOM - api platform('org.springframework.boot:spring-boot-dependencies:2.7.3') + api platform('org.springframework.boot:spring-boot-dependencies:2.7.5') // import Jackson's BOM - api platform('com.fasterxml.jackson:jackson-bom:2.13.2.20220328') + api platform('com.fasterxml.jackson:jackson-bom:2.13.4.20221013') // define all our dependencies versions constraints { // Internal modules dependencies (Keep them first) diff --git a/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/TemplateExampleMain.java b/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/TemplateExampleMain.java index 6b72e8246..6fe4e6aac 100644 --- a/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/TemplateExampleMain.java +++ b/symphony-bdk-examples/bdk-core-examples/src/main/java/com/symphony/bdk/examples/TemplateExampleMain.java @@ -28,5 +28,16 @@ public static void main(String[] args) throws Exception { // display processed template content log.info(content); + + // load inline template + final Template inlineTemplate = bdk.messages().templates().newTemplateFromString("\n" + + " This is a complex message, that supports ${name} templating\n" + + ""); + + // process template with some vars and retrieve content + final String inlineContent = inlineTemplate.process(Collections.singletonMap("name", "Freemarker")); + + // display processed template content + log.info(inlineContent); } }