forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateEngine.java
More file actions
87 lines (77 loc) · 2.53 KB
/
TemplateEngine.java
File metadata and controls
87 lines (77 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import javax.annotation.Nonnull;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
/**
* Template engine renderer. This class renderer instances of {@link ModelAndView} objects.
* Template engine rendering is done by checking view name and supported file {@link #extensions()}.
*
* @since 2.0.0
* @author edgar
*/
public interface TemplateEngine extends MessageEncoder {
/** Name of application property that defines the template path. */
String TEMPLATE_PATH = "templates.path";
/** Default template path. */
String PATH = "views";
/**
* Render a model and view instance as String.
*
* @param ctx Web context.
* @param modelAndView Model and view.
* @return Rendered template.
* @throws Exception If something goes wrong.
*/
String render(Context ctx, ModelAndView modelAndView) throws Exception;
@Override default byte[] encode(@Nonnull Context ctx, @Nonnull Object value) throws Exception {
// initialize flash and session attributes (if any)
ctx.flash();
ctx.sessionOrNull();
ctx.setDefaultResponseType(MediaType.html);
String output = render(ctx, (ModelAndView) value);
return output.getBytes(StandardCharsets.UTF_8);
}
/**
* True if the template engine is able to render the given view. This method checks if the view
* name matches one of the {@link #extensions()}.
*
* @param modelAndView View to check.
* @return True when view is supported.
*/
default boolean supports(@Nonnull ModelAndView modelAndView) {
String view = modelAndView.getView();
for (String extension : extensions()) {
if (view.endsWith(extension)) {
return true;
}
}
return false;
}
/**
* Number of file extensions supported by the template engine. Default is <code>.html</code>.
*
* @return Number of file extensions supported by the template engine.
* Default is <code>.html</code>.
*/
default @Nonnull List<String> extensions() {
return Collections.singletonList(".html");
}
/**
* Normalize a template path by removing the leading `/` when present.
*
* @param templatesPath Template path.
* @return Normalized path.
*/
static @Nonnull String normalizePath(@Nonnull String templatesPath) {
if (templatesPath == null) {
return null;
}
return templatesPath.startsWith("/") ? templatesPath.substring(1) : templatesPath;
}
}