Meta tag decorator

755 views
Skip to first unread message

Corrado Alesso

unread,
Oct 31, 2011, 9:41:56 PM10/31/11
to sitemes...@googlegroups.com
Hi,

in sitemesh 2 I was able to configure a PageDecoratorMapper and have
the decorator be choosen on the meta tag defined in the page.

For example this:

<meta name="decorator" content="error">

will render the page using the "error" decorator.

Is this still possbile in sitemesh 3?

Thanks,
Corrado

(I did not forget about the JSP tag implementation, but I've been
overwhelmed by some other tasks)

Joe Walnes

unread,
Nov 1, 2011, 8:18:35 AM11/1/11
to sitemes...@googlegroups.com
To do this, you have to create your own DecoratorSelector implementation.


  public class MetaTagDecoratorSelector implements DecoratorSelector<WebAppContext> {

    private final DecoratorSelector<WebAppContext> fallbackSelector;

    public MetaTagDecoratorSelector(DecoratorSelector<WebAppContext> fallbackSelector) {
      this.fallbackSelector = fallbackSelector;
    }

    @Override
    public String[] selectDecoratorPaths(Content content, WebAppContext context) {

      // Fetch <meta name=decorator> value.
      // The default HTML processor already extracts these into 'meta.NAME' properties.
      String decorator = content.getExtractedProperties()
                                .getChild("meta")
                                .getChild("decorator")
                                .getValue();

      if (decorator != null) {
        // If present, return it. 
        // Multiple chained decorators can be specified using commas.
        return decorator.split(","); 
      } else {
        // Otherwise, fallback to the standard configuration.
        return fallback.selectDecoratorPaths(content, context);
      }
    }
  }


To use it, you'll have to subclass the SiteMesh Filter.

  public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
      builder.setCustomDecoratorSelector(
        new MetaTagDecoratorSelector(builder.getDecoratorSelector());
    }
  }

Then use that Filter instead of ConfigurableSiteMeshFilter.

A couple of implementation differences between the above selector and the one that came with SM2:
* The meta tag will have to include the complete path, as SM3 no longer has the concept of decorator names.
  e.g. <meta name="decorator" content="/decorators/foo.jsp">
* If you wanted to use names, you could always use a map inside your custom decorator selector.
* You can specify multiple decorators that will get applied one after each other.
  e.g. <meta name="decorator" content="/decorators/two-column-layout.jsp,/decorators/branded-header.jsp">

This seems like a lot more work than in SM2. It is. But the design decision behind it is to provide a core library with lots of extensible hooks so users can do whatever they want. However, if there's enough demand for this approach, we could always bundle this DecoratorSelector with SM3.

Hope that helps.
-Joe

Corrado Alesso

unread,
Nov 1, 2011, 9:42:03 AM11/1/11
to sitemes...@googlegroups.com
Whoa, thanks for the super reply!

Trying this right now...

Thanks!


2011/11/1 Joe Walnes <j...@walnes.com>:

Reply all
Reply to author
Forward
0 new messages