In this section you will learn how to load Mustache templates from the file system, classpath, web context and more.
public interface TemplateLoader {
String DEFAULT_PREFIX = "/";
String DEFAULT_SUFFIX = ".hbs";
String getPrefix();
String getSuffix();
TemplateSource sourceAt(final String location) throws IOException;
String resolve(final String location);
}
absolute path
of a template.
hello
withprefix=/site
andsuffix=.html
get resolved to/site/hello.html
Template template = handlebars.compile("hello");
The exactly same rules applies forpartials
{{> hello}}
You don't need to addprefix
(like a home directory) andsuffix
(like a file extension), theTemplateLoader
does it for you.
The default value ofprefix
depends on a specific implementation ofTemplateLoader
.
The default value ofsuffix
is .hbs, but as you saw before, you can change to.html
or whatever make more sense to you (null included).
public interface TemplateSource {
String content() throws IOException;
Reader reader() throws IOException;
String filename();
long lastModified();
}
You don't have to implement any of these classes, Handlebars.java does it for you. But if you want to store your templates in a database (for example), these are the two classes you need to implement.
The ClassPathTemplateLoader is the default template loader in Handlebars.java. If you don't set a template loader, Handlebars.java assume your templates are in the classpath.
For example:
/
(a.k.a root of classpath) and
the default suffix is.hbs
(i.e. file extension is .hbs).
Handlebars handlebars = new Handlebars();
handlebars.compile("hello"); // resolved to: /hello.hbs
root of classpath
and.html
as file extension.
TemplateLoader loader = new ClassPathTemplateLoader("/", ".html");
Handlebars handlebars = new Handlebars(loader);
handlebars.compile("hello"); // resolved to: /hello.html
/templates
and.html
as file extension.
TemplateLoader loader = new ClassPathTemplateLoader("/templates", ".html");
Handlebars handlebars = new Handlebars(loader);
handlebars.compile("hello"); // resolved to: /templates/hello.html
The FileTemplateLoader load templates from a file system location. This template loader is useful if you need to make change to your templates after a deploy.
For example:
/usr/share/templates
and.html
as file extension.
TemplateLoader loader = new FileTemplateLoader("/usr/share/templates",
".html");
Handlebars handlebars = new Handlebars(loader);
handlebars.compile("hello"); // resolved to: /usr/share/templates/hello.html
The ServletContextTemplateLoader load templates from a JavaEE Web Context.
For example:
root of the web context
and.hbs
as file extension.
ServletContext servletContext = ...;
TemplateLoader loader = new ServletContextTemplateLoader(servletContext);
Handlebars handlebars = new Handlebars(loader);
handlebars.compile("hello"); // resolved to: /hello.hbs
/
and.html
as file extension.
ServletContext servletContext = ...;
TemplateLoader loader = new ServletContextTemplateLoader(servletContext, "/",
".html");
Handlebars handlebars = new Handlebars(loader);
handlebars.compile("hello"); // resolved to: /hello.html
/WEB-INF/pages
and.html
as file extension.
ServletContext servletContext = ...;
TemplateLoader loader = new ServletContextTemplateLoader(servletContext,
"/WEB-INF/pages", ".html");
Handlebars handlebars = new Handlebars(loader);
handlebars.compile("hello"); // resolved to: /WEB-INF/pages/hello.html
The CompositeTemplateLoader let you combine two or more template loaders and makes possible multiples templates location for your application.
For example:
ClassPathTemplateLoader cploader = ...;
FileTemplateLoader fsloader = ...;
Handlebars handlebars = new Handlebars().with(cploader, fsloader);
handlebars.compile("hello");
Loaders are executed in the order they are provided, so the first TemplateLoader that is able to resolved a template win.
TheTemplateLoader
andTemplateSource
are two key abstractions in Handlebars.java, they both work together for
simplify the storage and resolution of templates.
Thank you, for reading the Handlebars.java blog.