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