Freemarker as HST rendering template
The common templating engine for HST sites is JSP. Most Java web developers know it and there are plenty of libraries available. However JSP is not the only possibility to render your page. Woonsan wrote blogposts about using Spring MVC with the HST. Another flexible templating engine is Freemarker. Since we handle most of the logic in the HST component, the rendering template only needs to iterate over lists, check for empty variables and display values from beans. For this little bit of programming Freemarker is just as easy to use as JSP and IDEs like IntelliJ have code completion for it.
A few examples
In these examples we render the title of a bean called "mybean" if it contains a value. Then we iterate over a list and print each item. The last part is creating a link to a different bean.
JSP
<c:if test="${not empty mybean.title}"><h1>${mybean.title}</c:if>
<ul>
<c:forEach var="item" items="${mybean.items}">
<li>${item}</li>
</c:forEach>
</ul>
<hst:link hippobean="${otherbean}" var="link"/>
<a href="${link}">${otherbean.title}</a>
Freemarker
Freemarker supports both <>
and []
as syntax. I prefer to use the square brackets to distinguish the Freemarker instructions from the HTML.
[#if mybean.title?has_content]<h1>${mybean.title}</h1>[/#if]
<ul>
[#list mybean.items as item]
<li>${item}</li>
[/#list]
</ul>
[#assign link][@hst.link hippobean=otherbean/][/#assign]
<a href="${link}">${otherbean.title}</a>
If you’re not reading this post through RSS or Atom, you’re looking at a page that uses several Freemarker templates for the rendering. So, if templates in JSP and Freemarker are just as easy to create, why would you use Freemarker if you’re familiar with JSP? There’s one big difference: Hippo Site Toolkit supports Freemarker templates that are stored inside the Hippo Repository. This means you can directly modify your templates without having to deploy a new version of your web application. I will blog about this later because the mechanism for fetching the template will change in the next HST version.