This module, intended for use with Apache Isis, provides a wrapper around Freemarker templating engine.
You can either use this module "out-of-the-box", or you can fork this repo and extend to your own requirements.
To use "out-of-the-box":
-
update your classpath by adding this dependency in your dom project’s
pom.xml
:<dependency> <groupId>org.isisaddons.module.freemarker</groupId> <artifactId>isis-module-freemarker-dom</artifactId> <version>1.14.0</version> </dependency>
-
update the
getModules()
method of yourAppManifest
:@Override public List<Class<?>> getModules() { return Arrays.asList( ... org.isisaddons.module.freemarker.dom.FreeMarkerModule.class, ... ); }
-
optionally, enable/disable JODA support using a configuration property (in
isis.properties
, or in theAppManifest
):isis.services.addons.freemarker.jodaSupport=true
If not specified, then JODA support is enabled.
If you want to use the current -SNAPSHOT
, then the steps are the same as above, except:
-
when updating the classpath, specify the appropriate -SNAPSHOT version:
<version>1.15.0-SNAPSHOT</version>
-
add the repository definition to pick up the most recent snapshot (we use the Cloudbees continuous integration service). We suggest defining the repository in a
<profile>
:<profile> <id>cloudbees-snapshots</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>snapshots-repo</id> <url>http://repository-estatio.forge.cloudbees.com/snapshot/</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </profile>
In order to understand the API of the service provided by this module (FreeMarkService
), it’s necessary to
understand a little of the API/SPI exposed by FreeMarker itself.
Freemarker’s design is that templates are identified by a template name and a version. Freemarker’s TemplateLoader
SPI is used to lookup the template text and to check whether that template text is stale; Freemarker automatically
caches any template text and only reads in new text if a "lastModified" property has changed. The sequence is:
-
calls `TemplateLoader#findTemplateSource(templateName)
to look up an object that can act as the source of the template text (can return a reader). For example, this templateName might just be a file name.
-
then calls
TemplateLoader#getLastModified(templateSource)
to determine the time (ie versoin) of this particular source. For example, if the template source is a file, then this would be the lastModified timestamp on this file.
-
if necessary, then calls
TemplateLoader#getReader(templateSource, encoding)
.Freemarker automatically caches template text, so it will only make this call if the templateSource returns a newer lastModified timestamp than cached.
When FreeMarker passes in the templateName to #findTemplateSource(.)
it also appends a locale, eg "_en_GB". The idea,
obviously, is that locale-specific versions of templates can be returned.
This module provides a single service, called FreeMarkerService
. It has the following API:
@DomainService(nature = NatureOfService.DOMAIN)
public class FreeMarkerService {
public String render(
String templateName, // (1)
long version, // (2)
String templateChars, // (3)
Object dataModel) // (4)
throws IOException, TemplateException;
}
-
the name/version of the template, corresponding directly to FreeMarker’s "templateName".
-
the
templateChars
is the actual template text itself -
the
dataModel
is the object whose values are interpolated by Freemarker into the template. This can either be a strongly typed DTO or just a simpleMap
.
This method takes parameters that (from FreeMarker’s point of view) represent both the input to finding a template and also the output (the text of that template):
-
the
templateName
broadly corresponds directly to FreeMarker’s "templateName".To support multiple versions of a template over time, just create a composite name. Similarly, if multiple versions of a template are needed for different app tenancies, combine that into the composite name also.
For example, to support two versions of an "InvoiceTemplate" for France and for Italy, each published on 1 Dec 2015, one could pass in a template of "Invoice:/FRA:2015-12-01" or "Invoice:/ITA:2015-12-01".
-
the
templateChars
is the template text that is used to return a StringReader if and when FreeMarker calls#getReader(…)
.
Internally FreeMarker caches the template characters; it uses the templateName
and the version
as a way to
determine whether its internal cache is invalid. As a small wrinkle, it also performs the caching on a per-locale
basis; the templateName
used internally will have a locale appended to it (eg "_en_GB").
All this caching is irrelevant to the FreeMarkerService
, because it passes in the template characters irrespective; these
are simply made available on a thread-local. The responsibility for caching therefore moves outside of FreeMarker,
and to the calling application. Thus, the intended usage is that the template characters will be stored in an entity
(let’s call it DocumentTemplate
, say) which could be identified by documentType
and atPath
, say, and which also
is versioned. The documentType
and atPath
can simply be joined together to create the logical template name.
Note
|
FreeMarker also supports the notion of versioned templates (the |
From the unit tests:
// given
Map<String, String> properties = ImmutableMap.of("user", "John Doe");
// when
String merged = service.render("WelcomeUserTemplate:/GBR:2015-12-01:", "<h1>Welcome ${user}!</h1>", properties);
// then
assertThat(merged, is("<h1>Welcome John Doe!</h1>"));
-
1.14.0
- released against Isis 1.14.0 -
1.13.2
- released against Isis 1.13.2. Fixes #1 - automatic support for JODA dates and time (can be disabled using configuration property) -
1.13.1
- released against Isis 1.13.0. NB: this is a breaking change, with a simpler API. -
1.13.0
- released against Isis 1.13.0
Copyright 2016-2017 Dan Haywood
Licensed under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
Only the dom
module is deployed, and is done so using Sonatype’s OSS support (see
user guide).
To deploy a snapshot, use:
pushd dom
mvn clean deploy
popd
The artifacts should be available in Sonatype’s Snapshot Repo.
If you have commit access to this project (or a fork of your own) then you can create interim releases using the interim-release.sh
script.
The idea is that this will - in a new branch - update the dom/pom.xml
with a timestamped version (eg 1.14.0.20170227-0738
).
It then pushes the branch (and a tag) to the specified remote.
A CI server such as Jenkins can monitor the branches matching the wildcard origin/interim/*
and create a build.
These artifacts can then be published to a snapshot repository.
For example:
sh interim-release.sh 1.14.0 origin
where
-
1.14.0
is the base release -
origin
is the name of the remote to which you have permissions to write to.
The release.sh
script automates the release process. It performs the following:
-
performs a sanity check (
mvn clean install -o
) that everything builds ok -
bumps the
pom.xml
to a specified release version, and tag -
performs a double check (
mvn clean install -o
) that everything still builds ok -
releases the code using
mvn clean deploy
-
bumps the
pom.xml
to a specified release version
For example:
sh release.sh 1.14.0 \
1.15.0-SNAPSHOT \
dan@haywood-associates.co.uk \
"this is not really my passphrase"
where
-
$1
is the release version -
$2
is the snapshot version -
$3
is the email of the secret key (~/.gnupg/secring.gpg
) to use for signing -
$4
is the corresponding passphrase for that secret key.
Other ways of specifying the key and passphrase are available, see the `pgp-maven-plugin’s documentation).
If the script completes successfully, then push changes:
git push origin master && git push origin 1.14.0
If the script fails to complete, then identify the cause, perform a git reset --hard
to start over and fix the issue
before trying again. Note that in the dom’s `pom.xml
the nexus-staging-maven-plugin
has the
autoReleaseAfterClose
setting set to true
(to automatically stage, close and the release the repo). You may want
to set this to false
if debugging an issue.
According to Sonatype’s guide, it takes about 10 minutes to sync, but up to 2 hours to update search.