Roll Your Own Mustache Template Resolver in Bootique MVC

I recently blogged about my Bootique Mustache MVC Example Project on GitHub here. If you recall we touched on displaying Mustache Templates with Bootique AbstractView. That works well, but with the caveat that Mustache Templates must be in the same package as the View.

Resources/Templates/*.html

What if you would rather use a Template Resolver with your Mustache Templates in your resources/templates directory as .html files?

The answer is that while Bootique does the work of integrating Jersey and Mustache into the Bootique MVC Module for you, you can use the frameworks any way you want, the same as with any Bootique Module.

The MVC Module uses the Spullara Mustache Implementation, so we can use the Spullara Template Resolvers if we want. There are three Resolvers: DefaultResolver, ClasspathResolver and FileSystemResolver.

Our TemplatePathResolver Class is going to extend ClasspathResolver and implement MustacheResolver. The action takes place in our populateTemplate() method where we use ClasspathResolver's getReader() adding our custom /templates to the root path.

With our Template Resolver in place, packaging a template and returning an MVC response takes a few lines of code.

Source Code Notes for this Post

Source code discussed in this post is found in NixMash Microservices on GitHub. Code for this particular post is found in branch microservices-v0.1.0 - Initial Layout and Structure.

Posted July 19, 2017 02:22 PM EDT

More Like This Post