Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inline template support in TemplateEngine #686

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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