GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing jknack.github.io — {{ Handlebars.java }} - Where is my Mustache template? Where is my {{ Mustache }} template?In this section you will learn how to load Mustache templates from the file system, classpath, web context and more. The template loader
Template are loaded using the TemplateLoader API, which looks as follows:
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); } Prefix and SuffixThese two properties control theabsolute pathof a template.
For example, a template
hellowithprefix=/siteandsuffix=.htmlget resolved to/site/hello.html
Template template = handlebars.compile("hello");
The exactly same rules applies forpartials
{{> hello}}
You don't need to add
The default value of
The default value of The template source
A template source is a lightweight reference to a physical template stored somewhere. The TemplateSource class, looks as follows:
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 classpath template loaderThe 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:
The default prefix is
/(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
Setting prefix to the
root of classpathand.htmlas file extension.
TemplateLoader loader = new ClassPathTemplateLoader("/", ".html"); Handlebars handlebars = new Handlebars(loader); handlebars.compile("hello"); // resolved to: /hello.html
Setting prefix to the
/templatesand.htmlas file extension.
TemplateLoader loader = new ClassPathTemplateLoader("/templates", ".html"); Handlebars handlebars = new Handlebars(loader); handlebars.compile("hello"); // resolved to: /templates/hello.html The file template loaderThe 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:
Setting prefix to the
/usr/share/templatesand.htmlas 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 servlet context template loaderThe ServletContextTemplateLoader load templates from a JavaEE Web Context. For example:
Setting prefix to the
root of the web contextand.hbsas file extension.
ServletContext servletContext = ...; TemplateLoader loader = new ServletContextTemplateLoader(servletContext); Handlebars handlebars = new Handlebars(loader); handlebars.compile("hello"); // resolved to: /hello.hbs
Setting prefix to
/and.htmlas file extension.
ServletContext servletContext = ...; TemplateLoader loader = new ServletContextTemplateLoader(servletContext, "/", ".html"); Handlebars handlebars = new Handlebars(loader); handlebars.compile("hello"); // resolved to: /hello.html
Setting prefix to
/WEB-INF/pagesand.htmlas 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 composite template loaderThe CompositeTemplateLoader let you combine two or more template loaders and makes possible multiples templates location for your application. For example:
Using ClassPathTemplateLoader loader within a FileTemplateLoader:
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. Conclusion
The Want to contribute?
Thank you, for reading the Handlebars.java blog. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |