FreeMarker tutorial fixes

122 views
Skip to first unread message

dde...@freemail.hu

unread,
Jul 2, 2013, 7:23:19 AM7/2/13
to vog...@googlegroups.com
Hi,

Since your tutorial (http://www.vogella.com/articles/FreeMarker/article.html) is among the top hits for "freemarker" in Google, I would like to help you to fix some FreeMarker-related issues in it (I'm the current maintainer of FreeMarker.)

"By default FreeMarker searches in the classpath for templates."

It doesn't. But the default template-loader doesn't mater really, it's an arcane detail, and people should set the template-loader explicitly anyway. You could just say that: "It's configurable if from where FreeMarker reads the templates; commonly used options are loading from a directory, from the class-path, or from the servlet context."

This FTL snippet (occurs twice in the article):

"
<#if container??>
<div ="${container}">
<#else>
<div ="default">
</#if>
"

I guess that meant to be <div class="..."> (the "class" is missing). It's also not a good example, as this all should be written as a single line:

<div class="${container!'default'}">

To demonstrate #if/#else, there's an opportunity around the system listing:

<#if systems?size != 0>
<p>Systems:</p>
<ul>
<#list systems as system>
<li>${system_index + 1}.${system.name}</li>
</#list>
</ul>
<#else>
<p>No systems available</p>
</#if>

Also, you are using "system.getName()" at many places. That should be written as "${system.name}", since "name" is a JavaBean property.

Please call cfg.setXXX methods *directly* under "new Configuration()", so the whole part that configures FM is well separated (like with anempty line) from all the others. (It's common mistake with grave consequences to re-configure FM before each cfg.getTemplate, that's why.) This will be easier to do if you get rid of that "try" and its "catch" boilerplate, and just add "throws Exception" to the main method; that would make the example code more focused too.

Also, you should move the cfg.getTemplate down where the template is actually first needed (before the first template.process); cleaner, and this is also how it happens in real apps (you build a data-model first).

You don't need any of the flush() calls; FreeMarker auto-flushes at the end of process.

As you don't set the working directory yet you are using a relative path in "cfg.setDirectoryForTemplateLoading(new File("templates"))". It's unreliable, readers will burn themselves this way. I say, just create a Java package called "templates" inside the package of MainTest, and use cfg.setClassForTemplateLoading(MainTest.class, "templates"). That always works, and that way your templates are packed together with the classes (in a jar), which is also nice.

Lastly, the official Web page is freemarker.org, not freemarker.sourceforge.net

Thanks!

Lars Vogel

unread,
Jul 2, 2013, 3:21:59 PM7/2/13
to vog...@googlegroups.com
Hi Daniel,

a great honor to get feedback directly from you. This week I'm relatively busy but I try to apply your suggestions next week.

Thanks again, Lars

--
You received this message because you are subscribed to the Google Groups "vogella" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vogella+u...@googlegroups.com.
To post to this group, send email to vog...@googlegroups.com.
Visit this group at http://groups.google.com/group/vogella.
For more options, visit https://groups.google.com/groups/opt_out.



Reply all
Reply to author
Forward
0 new messages