Skip to content

Commit

Permalink
Add inline template to TemplateEngine
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yinan-symphony committed Nov 15, 2022
1 parent d47c1c3 commit 3639584
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gradle-wrapper-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions docs/message.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
```
<messageML>Hello, ${name}!</messageML>
Expand Down Expand Up @@ -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("<messageML>Hello, ${name}!</messageML>");
final String content = template.process(Collections.singletonMap("name", "Freemarker"));
log.info(content);
}
}
```

----
[Home :house:](./index.md)
4 changes: 2 additions & 2 deletions symphony-bdk-bom/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<messageML>\n"
+ " This is a complex message, that supports ${name} templating\n"
+ "</messageML>");

// process template with some vars and retrieve content
final String inlineContent = inlineTemplate.process(Collections.singletonMap("name", "Freemarker"));

// display processed template content
log.info(inlineContent);
}
}

0 comments on commit 3639584

Please sign in to comment.