55 */
66package io .jooby .handlebars ;
77
8- import com .github .jknack .handlebars .Decorator ;
98import com .github .jknack .handlebars .Handlebars ;
10- import com .github .jknack .handlebars .Helper ;
119import com .github .jknack .handlebars .cache .HighConcurrencyTemplateCache ;
1210import com .github .jknack .handlebars .cache .NullTemplateCache ;
1311import com .github .jknack .handlebars .cache .TemplateCache ;
2220import io .jooby .TemplateEngine ;
2321
2422import javax .annotation .Nonnull ;
25- import java .io .File ;
26- import java .io .IOException ;
27- import java .io .InputStream ;
28- import java .io .Reader ;
29- import java .net .URI ;
3023import java .net .URL ;
3124import java .nio .charset .StandardCharsets ;
3225import java .nio .file .Files ;
3629import static io .jooby .TemplateEngine .TEMPLATE_PATH ;
3730import static io .jooby .TemplateEngine .normalizePath ;
3831
32+ /**
33+ * Handlebars module: https://jooby.io/modules/handlebars.
34+ *
35+ * Usage:
36+ *
37+ * <pre>{@code
38+ * {
39+ *
40+ * install(new HandlebarsModule());
41+ *
42+ * get("/", ctx -> {
43+ * User user = ...;
44+ * return new ModelAndView("index.hbs")
45+ * .put("user", user);
46+ * });
47+ * }
48+ * }</pre>
49+ *
50+ * The template engine looks for a file-system directory: <code>views</code> in the current
51+ * user directory. If the directory doesn't exist, it looks for the same directory in the project
52+ * classpath.
53+ *
54+ * You can specify a different template location:
55+ *
56+ * <pre>{@code
57+ * {
58+ *
59+ * install(new HandlebarsModule("mypath"));
60+ *
61+ * }
62+ * }</pre>
63+ *
64+ * The <code>mypath</code> location works in the same way: file-system or fallback to classpath.
65+ *
66+ * Direct access to {@link Handlebars} is available via require call:
67+ *
68+ * <pre>{@code
69+ * {
70+ *
71+ * Handlebars hbs = require(Handlebars.class);
72+ *
73+ * }
74+ * }</pre>
75+ *
76+ * Complete documentation is available at: https://jooby.io/modules/handlebars.
77+ *
78+ * @author edgar
79+ * @since 2.0.0
80+ */
3981public class HandlebarsModule implements Extension {
4082
83+ /**
84+ * Utility class for creating {@link Handlebars} instances.
85+ */
4186 public static class Builder {
4287
43- private Handlebars handlebars ;
88+ private Handlebars handlebars = new Handlebars ()
89+ .setCharset (StandardCharsets .UTF_8 );
4490
4591 private TemplateLoader loader ;
4692
4793 private TemplateCache cache ;
4894
4995 private String templatesPath = TemplateEngine .PATH ;
5096
51- public Builder () {
52- handlebars = new Handlebars ();
53- handlebars .setCharset (StandardCharsets .UTF_8 );
54- }
55-
97+ /**
98+ * Set template cache.
99+ *
100+ * @param cache Template cache.
101+ * @return This builder.
102+ */
56103 public @ Nonnull Builder setTemplateCache (@ Nonnull TemplateCache cache ) {
57104 this .cache = cache ;
58105 return this ;
59106 }
60107
108+ /**
109+ * Template path.
110+ *
111+ * @param templatesPath Set template path.
112+ * @return This builder.
113+ */
61114 public @ Nonnull Builder setTemplatesPath (@ Nonnull String templatesPath ) {
62115 this .templatesPath = templatesPath ;
63116 return this ;
64117 }
65118
119+ /**
120+ * Template loader to use.
121+ *
122+ * @param loader Template loader to use.
123+ * @return This builder.
124+ */
66125 public @ Nonnull Builder setTemplateLoader (@ Nonnull TemplateLoader loader ) {
67126 this .loader = loader ;
68127 return this ;
69128 }
70129
71- public @ Nonnull <H > Builder registerHelper (@ Nonnull String name , @ Nonnull Helper <H > helper ) {
72- handlebars .registerHelper (name , helper );
73- return this ;
74- }
75-
76- public @ Nonnull <H > Builder registerHelperMissing (@ Nonnull Helper <H > helper ) {
77- handlebars .registerHelperMissing (helper );
78- return this ;
79- }
80-
81- public @ Nonnull Builder registerHelpers (@ Nonnull Object helperSource ) {
82- handlebars .registerHelpers (helperSource );
83- return this ;
84- }
85-
86- public @ Nonnull Builder registerHelpers (@ Nonnull Class <?> helperSource ) {
87- handlebars .registerHelpers (helperSource );
88- return this ;
89- }
90-
91- public @ Nonnull Builder registerHelpers (@ Nonnull URI location ) throws Exception {
92- handlebars .registerHelpers (location );
93- return this ;
94- }
95-
96- public @ Nonnull Builder registerHelpers (@ Nonnull File input ) throws Exception {
97- handlebars .registerHelpers (input );
98- return this ;
99- }
100-
101- public @ Nonnull Builder registerHelpers (@ Nonnull String filename , @ Nonnull Reader source )
102- throws Exception {
103- handlebars .registerHelpers (filename , source );
104- return this ;
105- }
106-
107- public @ Nonnull Builder registerHelpers (@ Nonnull String filename , @ Nonnull InputStream source )
108- throws Exception {
109- handlebars .registerHelpers (filename , source );
110- return this ;
111- }
112-
113- public @ Nonnull Builder registerHelpers (@ Nonnull String filename , @ Nonnull String source )
114- throws IOException {
115- handlebars .registerHelpers (filename , source );
116- return this ;
117- }
118-
119- public @ Nonnull Builder registerDecorator (@ Nonnull String name , @ Nonnull Decorator decorator ) {
120- handlebars .registerDecorator (name , decorator );
121- return this ;
122- }
123-
130+ /**
131+ * Creates a handlebars instance.
132+ *
133+ * @param env Application environment.
134+ * @return A new handlebars instance.
135+ */
124136 public @ Nonnull Handlebars build (@ Nonnull Environment env ) {
125137 if (loader == null ) {
126138 String templatesPath = normalizePath (env .getProperty (TEMPLATE_PATH , this .templatesPath ));
@@ -158,14 +170,28 @@ private static TemplateLoader defaultTemplateLoader(Environment env, String temp
158170
159171 private String templatesPath ;
160172
173+ /**
174+ * Creates a new handlebars module.
175+ *
176+ * @param handlebars Handlebars instance to use.
177+ */
161178 public HandlebarsModule (@ Nonnull Handlebars handlebars ) {
162179 this .handlebars = handlebars ;
163180 }
164181
182+ /**
183+ * Creates a new handlebars module.
184+ *
185+ * @param templatesPath Template location to use. First try to file-system or fallback to
186+ * classpath.
187+ */
165188 public HandlebarsModule (@ Nonnull String templatesPath ) {
166189 this .templatesPath = templatesPath ;
167190 }
168191
192+ /**
193+ * Creates a new handlebars module using the default path: <code>views</code>.
194+ */
169195 public HandlebarsModule () {
170196 this (TemplateEngine .PATH );
171197 }
@@ -180,7 +206,12 @@ public HandlebarsModule() {
180206 services .put (Handlebars .class , handlebars );
181207 }
182208
183- public static HandlebarsModule .Builder create () {
209+ /**
210+ * Creates a new freemarker builder.
211+ *
212+ * @return A builder.
213+ */
214+ public static @ Nonnull HandlebarsModule .Builder create () {
184215 return new HandlebarsModule .Builder ();
185216 }
186217}
0 commit comments