如何在 Play Framework 2 中实现主题支持?我想创建像这样的目录结构:views/default <- default template directory
views/site1 <- template for site 1
views/site2 <- template for site 2
如果模板不存在(即views/site1/home),它应该从默认目录呈现模板。
我试过cls = Class.forName("views.html.home);
但我得到类未找到异常。
解决方案:也许有人会发现这很有用:
protected static String renderTemplate(final String template, final String action,final ViewData templateParams) {
Class<?> cls = null;
String ret = "Template not found";
try {
cls = Class.forName(template);
} catch (ClassNotFoundException e) {
ret = e.toString();
}
if (cls == null) {
try {
cls = Class.forName("views.html.default."+action);
} catch (ClassNotFoundException e) {
ret = e.toString();
}
}
if (cls != null) {
Method htmlRender;
try {
htmlRender = cls.getMethod("render", ViewData.class);
ret = htmlRender.invoke("",templateParams).toString();
} catch (NoSuchMethodException e) {
ret = "Method not found"+e.toString();
} catch (IllegalAccessException e) {
ret = "illegal access exception";
} catch (InvocationTargetException e) {
ret = "InvocationTargetException";
}
}
return ret;
}
ViewData vd=new ViewData();
renderTemplate("views.html.custom."+viewname, actionname, vd)