[
https://issues.apache.org/jira/browse/WW-4145?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13723935#comment-13723935
]
Jasper Rosenberg commented on WW-4145:
--------------------------------------
Hmmm, tried implementing this as a TemplateLoader but it is a bit nastier than
I hoped, requiring passing the current template's theme hierarchy via thread
local. I haven't been able to test it yet, but it is pretty straight forward.
To integrate:
1. Modify FreemakerManager.createTemplateLoader() to wrap its current result in
the new ThemeTemplateLoader()
2. Modify FreemarkerTemplateEngine.renderTemplate() to set and clear (in
finally) the thread local on ThemeTemplateLoader with the list of possible
templates around the iteration over the possible templates. (this is cheaty but
efficient since the possible templates are the hierarchy, right?)
{code:java}
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.struts2.components.template.Template;
import freemarker.cache.TemplateLoader;
/**
* This template loader iterates over the given theme hierarchy, and tries to
find the first
* that when replacing the given token name actually exists.
*/
public class ThemeTemplateLoader implements TemplateLoader {
private static final ThreadLocal<List<Template>> THEME_HIERARCHY =
new ThreadLocal<List<Template>>();
private final String themeToken;
private final TemplateLoader parent;
public ThemeTemplateLoader(String themeToken, TemplateLoader parent) {
this.themeToken = themeToken;
this.parent = parent;
}
/** Set thread local theme hierarchy. */
public static void setThemeHierarchy(List<Template> themeHierarchy) {
THEME_HIERARCHY.set(themeHierarchy);
}
/** Clear thread local theme hierarchy. */
public static void clearThemeHierarch() {
THEME_HIERARCHY.remove();
}
/** {@inheritDoc} */
public Object findTemplateSource(String name) throws IOException {
int tokenIndex = (name == null) ? -1 : name.indexOf(themeToken);
if (tokenIndex < 0) {
return parent.findTemplateSource(name);
}
String prefix = name.substring(0, tokenIndex);
String suffix = name.substring(tokenIndex + themeToken.length());
List<Template> themeHierarchy = THEME_HIERARCHY.get();
for (Template template : themeHierarchy) {
String fullName = prefix + template.getTheme() + suffix;
Object templateSource = parent.findTemplateSource(fullName);
if (templateSource != null) {
return templateSource;
}
}
return null;
}
/** {@inheritDoc} */
public long getLastModified(Object templateSource) {
return parent.getLastModified(templateSource);
}
/** {@inheritDoc} */
public Reader getReader(Object templateSource, String encoding) throws
IOException {
return parent.getReader(templateSource, encoding);
}
/** {@inheritDoc} */
public void closeTemplateSource(Object templateSource) throws IOException {
parent.closeTemplateSource(templateSource);
}
}
{code}
> file.ftl in xhtml theme directly references xhtml controlfooter.ftl
> -------------------------------------------------------------------
>
> Key: WW-4145
> URL: https://issues.apache.org/jira/browse/WW-4145
> Project: Struts 2
> Issue Type: Bug
> Components: Other
> Affects Versions: 2.3.15.1
> Reporter: Jasper Rosenberg
> Assignee: Lukasz Lenart
> Labels: freemarker, tags, xhtml
> Fix For: 2.3.16
>
>
> Should use $\{parameters.theme} instead so can be used in theme extension.
> {code}
> <#include "/${parameters.templateDir}/${parameters.theme}/controlheader.ftl"
> />
> <#include "/${parameters.templateDir}/simple/file.ftl" />
> <#include "/${parameters.templateDir}/${parameters.theme}/controlfooter.ftl"
> />
> {code}
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira