Skip to content

Commit 9f752b1

Browse files
committed
Refactor template path for handlebars and freemarker
1 parent 6e09311 commit 9f752b1

File tree

8 files changed

+74
-43
lines changed

8 files changed

+74
-43
lines changed

docs/asciidoc/modules/freemarker.adoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ import io.jooby.freemarker.Freemarker
5454
Template location is set to `views`. The `views` folder/location is expected to be at the current
5555
user directory or at root of classpath.
5656

57-
You can override the default location by setting the `freemarker.templatePath` property.
57+
You can override the default location by setting the `templates.path` property in the application
58+
configuration file or programmatically at creation time.
5859

5960
=== Template Cache
6061

@@ -79,7 +80,7 @@ Custom Configuration object can be provided it programmatically:
7980
.Java
8081
[source, java, role="primary"]
8182
----
82-
import io.jooby.handlebars.hbs;
83+
import io.jooby.freemarker.Freemarker;
8384
8485
{
8586
Configuration freemarker = new Configuration();

docs/asciidoc/modules/handlebars.adoc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ import io.jooby.handlebars.Hbs
5454
Template location is set to `views`. The `views` folder/location is expected to be at the current
5555
user directory or at root of classpath.
5656

57-
You can override the default location by setting the `handlebars.templatePath` property.
57+
You can override the default location by setting the `templates.path` property in the application
58+
configuration file or programmatically at creation time.
5859

5960
=== Template Cache
6061

@@ -68,10 +69,12 @@ Custom Handlebars object can be provided it programmatically:
6869
.Java
6970
[source, java, role="primary"]
7071
----
71-
import io.jooby.handlebars.hbs;
72+
import io.jooby.handlebars.Hbs;
7273
7374
{
7475
Handlebars handlebars = new Handlebars();
76+
77+
// Apply custom configuration
7578
7679
install(new Hbs(handlebars));
7780
}
@@ -80,11 +83,13 @@ import io.jooby.handlebars.hbs;
8083
.Kotlin
8184
[source, kt, role="secondary"]
8285
----
83-
import io.jooby.freemarker.Freemarker
86+
import io.jooby.handlebars.Hbs
8487
8588
{
86-
val handlebars = Handlebars();
89+
val handlebars = Handlebars()
8790
91+
// Apply custom configuration
92+
8893
install(Hbs(handlebars))
8994
}
9095
----

jooby/src/main/java/io/jooby/Environment.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ public Environment(@Nonnull ClassLoader classLoader, @Nonnull Config conf, @Nonn
7272
this.conf = conf;
7373
}
7474

75+
public @Nonnull String getProperty(@Nonnull String key, @Nonnull String defaults) {
76+
if (conf.hasPath(key)) {
77+
return conf.getString(key);
78+
}
79+
return defaults;
80+
}
81+
82+
public @Nullable String getProperty(@Nonnull String key) {
83+
if (conf.hasPath(key)) {
84+
return conf.getString(key);
85+
}
86+
return getProperty(key, null);
87+
}
88+
7589
/**
7690
* Application configuration.
7791
*

jooby/src/main/java/io/jooby/TemplateEngine.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
*/
1717
public interface TemplateEngine extends Renderer {
1818

19+
/** Name of application property that defines the template path. */
20+
String TEMPLATE_PATH = "templates.path";
21+
22+
/** Default template path. */
23+
String PATH = "views";
24+
1925
/**
2026
* Render a model and view instance as String.
2127
*
@@ -31,4 +37,8 @@ public interface TemplateEngine extends Renderer {
3137
String output = apply(ctx, (ModelAndView) value);
3238
return output.getBytes(StandardCharsets.UTF_8);
3339
}
40+
41+
static @Nonnull String normalizePath(@Nonnull String templatesPath) {
42+
return templatesPath.startsWith("/") ? templatesPath.substring(1) : templatesPath;
43+
}
3444
}

modules/jooby-freemarker/src/main/java/io/jooby/freemarker/Freemarker.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,24 @@
1212
import freemarker.core.HTMLOutputFormat;
1313
import freemarker.core.OutputFormat;
1414
import freemarker.template.Configuration;
15-
import freemarker.template.DefaultObjectWrapper;
1615
import freemarker.template.DefaultObjectWrapperBuilder;
1716
import freemarker.template.TemplateException;
18-
import freemarker.template.Version;
1917
import io.jooby.Environment;
2018
import io.jooby.Extension;
2119
import io.jooby.Jooby;
2220
import io.jooby.MediaType;
2321
import io.jooby.ServiceRegistry;
2422
import io.jooby.Sneaky;
23+
import io.jooby.TemplateEngine;
2524

2625
import javax.annotation.Nonnull;
2726
import java.nio.file.Files;
2827
import java.nio.file.Path;
2928
import java.nio.file.Paths;
30-
import java.util.Optional;
3129
import java.util.Properties;
3230

33-
import static io.jooby.Sneaky.throwingConsumer;
31+
import static io.jooby.TemplateEngine.TEMPLATE_PATH;
32+
import static io.jooby.TemplateEngine.normalizePath;
3433

3534
public class Freemarker implements Extension {
3635

@@ -42,7 +41,7 @@ public static class Builder {
4241

4342
private OutputFormat outputFormat = HTMLOutputFormat.INSTANCE;
4443

45-
private String templatePath;
44+
private String templatesPath = TemplateEngine.PATH;
4645

4746
public @Nonnull Builder setTemplateLoader(@Nonnull TemplateLoader loader) {
4847
this.templateLoader = loader;
@@ -59,12 +58,8 @@ public static class Builder {
5958
return this;
6059
}
6160

62-
public @Nonnull Builder setTemplatePath(@Nonnull String templatePath) {
63-
if (templatePath.startsWith("/")) {
64-
this.templatePath = templatePath.substring(1);
65-
} else {
66-
this.templatePath = templatePath;
67-
}
61+
public @Nonnull Builder setTemplatesPath(@Nonnull String templatesPath) {
62+
this.templatesPath = templatesPath;
6863
return this;
6964
}
7065

@@ -80,8 +75,7 @@ public static class Builder {
8075
conf.getConfig("freemarker").root().unwrapped()
8176
.forEach((k, v) -> settings.put(k, v.toString()));
8277
}
83-
String templatePath = Optional.ofNullable((String) settings.remove("templatePath"))
84-
.orElse(Optional.ofNullable(this.templatePath).orElse("views"));
78+
String templatesPath = normalizePath(env.getProperty(TEMPLATE_PATH, this.templatesPath));
8579

8680
settings.putIfAbsent("defaultEncoding", "UTF-8");
8781
/** Cache storage: */
@@ -92,12 +86,9 @@ public static class Builder {
9286

9387
freemarker.setSettings(settings);
9488

95-
/** Template path: */
96-
setTemplatePath(templatePath);
97-
9889
/** Template loader: */
9990
if (templateLoader == null) {
100-
templateLoader = defaultTemplateLoader(env);
91+
templateLoader = defaultTemplateLoader(env, templatesPath);
10192
}
10293
freemarker.setTemplateLoader(templateLoader);
10394

@@ -116,13 +107,13 @@ public static class Builder {
116107
}
117108
}
118109

119-
private TemplateLoader defaultTemplateLoader(Environment env) {
110+
private TemplateLoader defaultTemplateLoader(Environment env, String templatesPath) {
120111
try {
121-
Path templateDir = Paths.get(System.getProperty("user.dir"), templatePath);
112+
Path templateDir = Paths.get(System.getProperty("user.dir"), templatesPath);
122113
if (Files.exists(templateDir)) {
123114
return new FileTemplateLoader(templateDir.toFile());
124115
}
125-
return new ClassTemplateLoader(env.getClassLoader(), "/" + templatePath);
116+
return new ClassTemplateLoader(env.getClassLoader(), "/" + templatesPath);
126117
} catch (Exception x) {
127118
throw Sneaky.propagate(x);
128119
}
@@ -131,16 +122,23 @@ private TemplateLoader defaultTemplateLoader(Environment env) {
131122

132123
private Configuration freemarker;
133124

125+
private String templatesPath;
126+
134127
public Freemarker(@Nonnull Configuration freemarker) {
135128
this.freemarker = freemarker;
136129
}
137130

131+
public Freemarker(@Nonnull String templatesPath) {
132+
this.templatesPath = templatesPath;
133+
}
134+
138135
public Freemarker() {
136+
this(TemplateEngine.PATH);
139137
}
140138

141139
@Override public void install(@Nonnull Jooby application) {
142140
if (freemarker == null) {
143-
freemarker = create().build(application.getEnvironment());
141+
freemarker = create().setTemplatesPath(templatesPath).build(application.getEnvironment());
144142
}
145143
application.renderer(MediaType.html, new FreemarkerTemplateEngine(freemarker));
146144

modules/jooby-freemarker/src/test/java/io/jooby/freemarker/FreemarkerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void publicField() throws Exception {
7373
@Test
7474
public void customTemplatePath() throws Exception {
7575
Configuration freemarker = Freemarker.create()
76-
.build(new Environment(getClass().getClassLoader(), ConfigFactory.empty().withValue("freemarker.templatePath",
76+
.build(new Environment(getClass().getClassLoader(), ConfigFactory.empty().withValue("templates.path",
7777
ConfigValueFactory.fromAnyRef("foo"))));
7878
FreemarkerTemplateEngine engine = new FreemarkerTemplateEngine(freemarker);
7979
MockContext ctx = new MockContext();

modules/jooby-handlebars/src/main/java/io/jooby/handlebars/Hbs.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
1515
import com.github.jknack.handlebars.io.FileTemplateLoader;
1616
import com.github.jknack.handlebars.io.TemplateLoader;
17-
import com.typesafe.config.Config;
1817
import io.jooby.Environment;
1918
import io.jooby.Extension;
2019
import io.jooby.Jooby;
2120
import io.jooby.MediaType;
2221
import io.jooby.ServiceRegistry;
22+
import io.jooby.TemplateEngine;
2323

2424
import javax.annotation.Nonnull;
2525
import java.io.File;
@@ -32,7 +32,9 @@
3232
import java.nio.file.Files;
3333
import java.nio.file.Path;
3434
import java.nio.file.Paths;
35-
import java.util.Optional;
35+
36+
import static io.jooby.TemplateEngine.TEMPLATE_PATH;
37+
import static io.jooby.TemplateEngine.normalizePath;
3638

3739
public class Hbs implements Extension {
3840

@@ -44,7 +46,7 @@ public static class Builder {
4446

4547
private TemplateCache cache;
4648

47-
private String templatePath;
49+
private String templatesPath = TemplateEngine.PATH;
4850

4951
public Builder() {
5052
handlebars = new Handlebars();
@@ -56,8 +58,8 @@ public Builder() {
5658
return this;
5759
}
5860

59-
public @Nonnull Builder setTemplatePath(@Nonnull String templatePath) {
60-
this.templatePath = templatePath.startsWith("/") ? templatePath.substring(1) : templatePath;
61+
public @Nonnull Builder setTemplatesPath(@Nonnull String templatesPath) {
62+
this.templatesPath = templatesPath;
6163
return this;
6264
}
6365

@@ -121,14 +123,8 @@ public Builder() {
121123

122124
public @Nonnull Handlebars build(@Nonnull Environment env) {
123125
if (loader == null) {
124-
Config config = env.getConfig();
125-
String defaultTemplatePath = config.hasPath("handlebars.templatePath")
126-
? config.getString("handlebars.templatePath")
127-
: "views";
128-
String templatePath = Optional.ofNullable(this.templatePath)
129-
.orElse(defaultTemplatePath);
130-
setTemplatePath(templatePath);
131-
loader = defaultTemplateLoader(env, this.templatePath);
126+
String templatesPath = normalizePath(env.getProperty(TEMPLATE_PATH, this.templatesPath));
127+
loader = defaultTemplateLoader(env, templatesPath);
132128
}
133129
handlebars.with(loader);
134130

@@ -160,16 +156,23 @@ private static TemplateLoader defaultTemplateLoader(Environment env, String temp
160156

161157
private Handlebars handlebars;
162158

159+
private String templatesPath;
160+
163161
public Hbs(@Nonnull Handlebars handlebars) {
164162
this.handlebars = handlebars;
165163
}
166164

165+
public Hbs(@Nonnull String templatesPath) {
166+
this.templatesPath = templatesPath;
167+
}
168+
167169
public Hbs() {
170+
this(TemplateEngine.PATH);
168171
}
169172

170173
@Override public void install(@Nonnull Jooby application) throws Exception {
171174
if (handlebars == null) {
172-
handlebars = create().build(application.getEnvironment());
175+
handlebars = create().setTemplatesPath(templatesPath).build(application.getEnvironment());
173176
}
174177
application.renderer(MediaType.html, new HbsTemplateEngine(handlebars));
175178

modules/jooby-handlebars/src/test/java/io/jooby/handlebars/HbsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void render() throws Exception {
4848
@Test
4949
public void renderFileSystem() throws Exception {
5050
Handlebars handlebars = Hbs.create()
51-
.setTemplatePath(Paths.get("src", "test", "resources", "views").toString())
51+
.setTemplatesPath(Paths.get("src", "test", "resources", "views").toString())
5252
.build(new Environment(getClass().getClassLoader(), ConfigFactory.empty()));
5353
HbsTemplateEngine engine = new HbsTemplateEngine(handlebars);
5454
MockContext ctx = new MockContext();

0 commit comments

Comments
 (0)