{#========================================== Spincast Templating Addon plugin ==========================================#} {% extends "../../layout.html" %} {% block sectionClasses %}plugins plugins-spincast-templating-addon{% endblock %} {% block meta_title %}Plugins - Spincast Templating Addon{% endblock %} {% block meta_description %}Spincast Templating Addon plugin{% endblock %} {% block scripts %} {% endblock %} {% block body %}
The Spincast Templating Addon plugin provides a request context add-on:
"ITemplatingRequestContextAddon". This add-on allows your route handlers to
easily access templating functionnalities. It is mounted as .templating()
on the default Spincast request context.
If you use the spincast-default artifact, this plugin is already installed so
you have nothing more to do!
If you start from scratch using the spincast-core artifact, you can use the
plugin by adding this artifact to your project:
<dependency>
<groupId>org.spincast</groupId>
<artifactId>spincast-plugins-templating-addon</artifactId>
<version>{{spincastLatestStableVersion}}</version>
</dependency>
You then install the plugin's Guice module, by passing it to the Guice.createInjector(...) method:
Injector guice = Guice.createInjector(
new SpincastCoreGuiceModule(args),
new SpincastTemplatingAddonPluginGuiceModule(IAppRequestContext.class)
// other modules...
);
... or by using the install(...) method from your custom Guice module:
public class AppModule extends SpincastCoreGuiceModule {
@Override
protected void configure() {
super.configure();
install(new SpincastTemplatingAddonPluginGuiceModule(getRequestContextType()));
// other modules...
}
// ...
}
The add-on returns a ITemplatingEngine instance. Look at the
The ITemplatingEngine interface section
of the Spincast Pebble plugin (the default implementation of ITemplatingEngine) for a list of
methods available on this object!
public void myHandler(IAppRequestContext context) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("name", "Stromgol");
String html = context.templating().evaluate("<h1>Hi {{name}}!</h1>", params);
System.out.println(html); // <h1>Hi Stromgol!</h1>
}{% endverbatim %}