1212import freemarker .core .HTMLOutputFormat ;
1313import freemarker .core .OutputFormat ;
1414import freemarker .template .Configuration ;
15- import freemarker .template .DefaultObjectWrapper ;
1615import freemarker .template .DefaultObjectWrapperBuilder ;
1716import freemarker .template .TemplateException ;
18- import freemarker .template .Version ;
1917import io .jooby .Environment ;
2018import io .jooby .Extension ;
2119import io .jooby .Jooby ;
2220import io .jooby .MediaType ;
2321import io .jooby .ServiceRegistry ;
2422import io .jooby .Sneaky ;
23+ import io .jooby .TemplateEngine ;
2524
2625import javax .annotation .Nonnull ;
2726import java .nio .file .Files ;
2827import java .nio .file .Path ;
2928import java .nio .file .Paths ;
30- import java .util .Optional ;
3129import 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
3534public 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
0 commit comments