In a recent blog I created a little Java API for registering templates. With this approach you could define templates in external html files, which simplifies template management and editing (due to html editor support). It also gives you the benefit of loading templates on demand, and to minimze the size of your main html file. But it was still lacking a couple of things.
Here’s an improved way of doing it:
The github repository contains an example app using the API. To register a template you will again make a call to a Java API:
Closeable templ = TemplateRegistration.registerTemplate("a", "a.html");
But instead of loading it immediately, it will only be registered. Only when you first use it in a template binding, it will first be loaded. This two step-process is useful when using DukeScript in a module system like OSGi or the NetBeans Platform.
When a module is loaded, and activated it can register it’s templates, but they won’t be loaded (and use memory) until the template is actually used.
A second benefit compared to the original approach is, that you can also unregister a template thereby freeing up memory if the template is no longer in use, or the module is unloaded.
Closeable templ = TemplateRegistration.registerTemplate("a", "a.html"); // ... do something with it templ.close();// <-unregisters it