Skip to content

Commit

Permalink
Add inline template support in TemplateEngine (#686)
Browse files Browse the repository at this point in the history
This commit provides the inline template support in TemplateEgine,
so that the template can be initiated not only from a file, but could
be also from a template string.
  • Loading branch information
yinan-symphony authored Nov 14, 2022
1 parent cb78279 commit d47c1c3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public interface TemplateEngine {
*/
Template newTemplateFromClasspath(String templatePath);

/**
* Create a {@link Template} instance from a string
*
* @param template inline template string
* @return a new {@link Template} instantiated from the provided inline string
* @throws TemplateException when template cannot be loaded.
*/
Template newTemplateFromString(String template);

static TemplateEngine getDefaultImplementation() {
final ServiceLoader<TemplateEngine> engineServiceLoader = ServiceLoader.load(TemplateEngine.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* FreeMarker specific implementation of {@link TemplateEngine}. Instantiates {@link FreeMarkerTemplate} objects.
*
* <p>
* This class is thread-safe.
* This class is thread-safe.
* </p>
*/
@API(status = API.Status.INTERNAL)
Expand Down Expand Up @@ -53,6 +53,19 @@ public Template newTemplateFromClasspath(String templatePath) {
}
}

/**
* {@inheritDoc}
*/
@Override
public Template newTemplateFromString(String template) {
try {
final Configuration configuration = createConfiguration();
return new FreeMarkerTemplate(new freemarker.template.Template(null, template, configuration));
} catch (IOException e) {
throw new TemplateException("Unable to load template from string", e);
}
}

private static Configuration createConfiguration() {
final Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
cfg.setDefaultEncoding("UTF-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ public void testFromClasspathWithLong() {
"<messageML>Hello <mention uid='" + userId + "'/></messageML>\n");
}

@Test
public void testFromInlineString() {
Long userId = 12345678L;
Map<String, Object> parameters = new HashMap<>();
parameters.put("userId", userId);

Template freeMarkerTemplate = new FreeMarkerEngine().newTemplateFromString("<messageML>Hello <mention uid='${userId}'/></messageML>\n");
assertTemplateProducesOutput(freeMarkerTemplate, parameters,
"<messageML>Hello <mention uid='" + userId + "'/></messageML>\n");
}

@Test
public void testWithNotFoundResource() {
assertThrows(TemplateException.class, () -> new FreeMarkerEngine().newTemplateFromClasspath("./not/found.ftl"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
* {@link Handlebars} implementation of the {@link TemplateEngine} interface.
*
* <p>
* This class is thread-safe.
* This class is thread-safe.
* </p>
*/
@API(status = API.Status.INTERNAL)
public class HandlebarsEngine implements TemplateEngine {

/** Handlebars for classpath loading. Ok for thread-safety. */
/**
* Handlebars for classpath loading. Ok for thread-safety.
*/
private static final Handlebars HANDLEBARS = createHandlebars(new ClassPathTemplateLoader());

/**
Expand Down Expand Up @@ -54,6 +56,20 @@ public Template newTemplateFromClasspath(String templatePath) {
}
}

/**
* {@inheritDoc}
*/
@Override
public Template newTemplateFromString(String template) {
try {
return new HandlebarsTemplate(HANDLEBARS.compileInline(template));
} catch (IOException e) {
throw new TemplateException("Unable to compile Handlebars template from inline string: " + template, e);
}
}



/**
* Creates a new {@link Handlebars} object with suffix set to "" to make this {@link TemplateEngine} implementation
* consistent with other ones (e.g. developers have to specify the template resource extension).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ void should_load_template_from_file(@TempDir Path tempDir) throws Exception {
assertEquals(EXPECTED_TEST_HBS, content);
}

@Test
void should_load_template_from_inline_string() throws Exception {
final Template template = this.engine.newTemplateFromString("<messageML>\n{{message}}\n</messageML>\n");
final String content = template.process(Collections.singletonMap("message", "hello"));
assertEquals(EXPECTED_TEST_HBS, content);
}

@Test
void should_load_complex_template_from_file(@TempDir Path tempDir) throws Exception {

Expand Down

0 comments on commit d47c1c3

Please sign in to comment.