Some content

Freemarker & Snow

Snow use freemarker as templating engine which simple, efficient, and extensible. Here are the design points of the Freemarker-Snow integration:

  • A extension-less URI map to a freemarker template file with a ".ftl" extension. So, /path/to/page URI would map to /path/to/page.ftl file.
  • The model is built by one or more @WebModelHandler and can be used directly in the freemarker templates as freemarker models.
  • Snow add a built-in _r freemarker model to access request context information in freemarker code.
  • Snow provides some convenient built-in freemarker directives (see below).

Look at the freemarker documentation for more information about the freemarker syntax.

_r request object

When Snow process a freemarker page, Snow add _r as a page model object which contains all the information about the request.

For a URI such as http://localhost:8080/test_r-properties?param1=123

${_r.contextPath}: ""
${_r.fullPath}: "/test_r-properties"
${_r.href}: "/test_r-properties?param1=123"
${_r.pathInfo}: "/test_r-properties"
${_r.user}: "com.example.samplebookmarks.entity.User@1732781b"
${_r.param.param1}: "123"
${_r.req.getHeader("host")}: "localhost:8080" (_r.req is the 

Note Here is the test_r-properties.ftl source file. If you have the sampleBookmarks running on the default 8080 port, you can see those value in the test_r-properties?param1=123

Built-in Snow directives

[@includeFrameContent /]

Built-in Snow freemarker directive to be used in _frame.ftl to include the target page (or the next _frame.ftl matching the path hierarchy).

Typical _frame.ftl.

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Cool App</title>
    <link rel="stylesheet" type="text/css" href="${_r.contextPath}/bootstrap/css/bootstrap.css">
    <script type="text/javascript" src="${_r.contextPath}/js/jquery.min.js"></script>
  </head>    
  <body>
    [@includeFrameContent /]
  </body>
</html>    

See sampleBookmarks _frame.ftl for a sample usage.

[@includeTemplate name="..." /]

Built-in Snow freemarker directive to include another freemarker template by name (relative to the webapp root folder).

...
  [@includeTemplate name="some-freemarker-macros.ftl" /]
....
  [@includeTemplate name="navbar.ftl" /]

See sampleBookmarks bookmarks.ftl for a sample usage.