Search code examples
spring-mvcfreemarkersitemesh

How should I populate the page heading in my Sitemesh 3 template?


I'm using Sitemesh 3 and Freemarker with my Spring MVC application. Freemarker page output is passed to Sitemesh for application of a JSP decorator (will be FTL once I get my config sorted out - I have another SO question about that - as yet unanswered).

I'd got Sitemesh 3 applying the body to the template correctly, now I'd like to add to my setup so an FTL view can set a property that would be rendered in the page heading.

Sitemesh (JSP) template/decorator - this is producing correct output, except for the variable content heading:

    <div id="mainWrapper">
    <div id="content">
        <div id="contentheading"><sitemesh:write property="page.heading"></sitemesh:write></div>
        <div>
            <sitemesh:write property='body'/>
        </div>
    </div>
</div>

What do I need to do in my .ftl in order to create some property that would be rendered in the div#contentheading element?

I have a Java config for sitemesh using a custom filter implementation:

public class SitemeshFilter extends ConfigurableSiteMeshFilter {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
    builder.setMimeTypes("text/html", "application/xhtml+xml");
    builder.addDecoratorPath("/*", "/WEB-INF/templates/page.jsp");
  }
}

Please note I am using Sitemesh 3


Solution

  • A simple way to do this, that requires no additional SiteMesh configuration is by using a tag in your FTL view. The default SiteMesh 3 tag processor will automatically parse these tags and make them available to your decorator using a "meta." prefix.

    FTL view:

    <html>
      <head>
        <title>Some title</title>
        <meta name="heading" content="My page heading">
        ...
      </head>
      <body>
        ...
      </body>
    </html>
    

    Then in your decorator:

    <html>
      ...
      <div id="content">
        <div id="contentHeading"><sitemesh:write property="meta.heading"/></div>
        ....
      </div>
      ...