FreeMarker service

This service let you use the great template mechanism FreeMarker as injectable service in your Tapestry application.

Configuration

optionaly insert your individual FreeMarker configuration This may be null, so the service create its own minimalistic configuration.

public class AppModule
{
    public static void contributeFreeMarkerService(MappedConfiguration<String, freemarker.template.Configuration> configuration)
    {
        freemarker.template.Configuration fmConfiguration = new freemarker.template.Configuration();
        fmConfiguration.setWhitespaceStripping(false);
        config.setClassForTemplateLoading(fmConfiguration.getClass(), "/");
        configuration.add("freemarker.configuration", fmConfiguration);
    }
}

First Sample

here a easy sample to generate a email body with the FreeMarkerService

the template file

if you think i am crazy if you read the sample text, read this

Dear ${user_name},

you've tried ${login_tries} times to log into our system with wrong password!
for this reason your home planet would removed for an intergalactical highway
in 1000 years from today (${block_date?date}).

If you feel that be a fault, please send your antilogy to the ministry at Vogsphere,
or contact your <a href="mailto:${sysadm_email}">system administrator</a> via the babelfish.

dont panic and thanks for all the fish,
Zaphod Beeblebrox

the page class

public class PassengerList
{
    @Inject @FreeMarkerMarker
    private TemplateService _templateService;

    public void afterThirdTryToLogin()
    {
        OutputStream emailBodyStream = new EmailBodyStream();
        Map parameterMap = new HashMap();
        parameterMap.put("user_name", "Athur Dent");
        parameterMap.put("login_tries", getLoginTries());
        parameterMap.put("block_date", new Date());
        parameterMap.put("sysadm_email", "Zaphod.Beeblebrox@beteigeuze.behind.the.moon");
        _templateService.mergeDataWithResource(new URIResource("./templates/email_body.ftl"),
                                               emailBodyStream, parameterMap)

        Email email = createEmail(emailBodyStream);
        email.send();
    }
}

Second Sample

here a easy sample to generate a report with the FreeMarkerService

the template file

This is the passenger list on flight to
${target}.

=====================================================
<#list elementList as passenger>
${passenger.lastname}, ${passenger.firstname}
</#list>
=====================================================

they are mostly harmless!

the page class

public class LostPassword
{
    @Inject @FreeMarkerMarker
    private TemplateService _templateService;

    public StreamResponse getPassengerList()
    {
        List<Passenger> passengerList = getDAO(Passenger.class).findAllForFlight0815();
        OutputStream os = new OutputStream();
        Map parameterMap = new HashMap();
        parameterMap.put("target", "the restaurant at the end of the universe");
        _templateService.mergeDataWithResource(new URIResource("./templates/report.ftl"),os,
                                               parameterMap, passengerList)

        return TextStreamResponse("text/plain", os.toString())
    }
}