简体   繁体   English

Play Framework 2.0中的主题支持

[英]Theming support in Play Framework 2.0

How can i implement theming support in Play Framework 2? 如何在Play Framework 2中实现主题支持? I want to create directory structure like: views/default <- default template directory 我想创建目录结构,例如:views / default <-默认模板目录
views/site1 <- template for site 1 views / site1 <-网站1的模板
views/site2 <- template for site 2 views / site2 <-网站2的模板

If template doesn`t exist (ie. views/site1/home) it should render template from default directory. 如果模板不存在(即views / site1 / home),则应从默认目录渲染模板。

I have tried cls = Class.forName("views.html.home); But I get class not found exception. 我已经尝试过cls = Class.forName("views.html.home);但是我没有找到类异常。

SOLUTION: Maybe someone will find this useful: 解决方案:也许有人会发现这个有用:

 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) 

You have to implement it yourself, as a reference, check the Play Authenticate usage sample , it allows to send ie. 您必须自己实现它,作为参考,检查Play Authenticate使用示例 ,该示例允许发送ie。 validation emails basing on Play's template and depending on the client's language, so for an instance, if your main language is Polish it will render the verify_email_pl.scala.html otherwise if your browser uses language not supported by PA, it will silently fallback to: verify_email_en.scala.html . 验证电子邮件基于Play的模板,并取决于客户端的语言,因此,对于一个实例,如果您的主要语言是波兰语,则会呈现verify_email_pl.scala.html否则,如果您的浏览器使用的是PA不支持的语言,则会悄悄地回verify_email_pl.scala.htmlverify_email_en.scala.html

Check the usage and declaration of the method. 检查方法的用法声明

For your case it will be good solution, of course just criteria of the choice will be different. 对于您的情况,这将是一个很好的解决方案,当然,选择的标准将有所不同。

This process is called "Branding". 此过程称为“品牌推广”。 What you have to do is following. 您要做的是遵循。 Create a table in db by name "BRANDING" and add theme names in it against each instance of website. 在数据库中通过名称“ BRANDING”创建一个表,并针对每个网站实例在其中添加主题名称。 Now you will make folders hierarchy as you mentioned and in jsp pages where load css files you will do that like this <link rel="stylesheet" type="text/css" href="/views/${themeName}.css"> 现在,您将按照前面提到的那样在文件夹层次结构中创建文件夹层次结构,并在其中加载css文件的jsp页面中执行以下操作: <link rel="stylesheet" type="text/css" href="/views/${themeName}.css">

where themeName would be a server side variable that you will program in your controller to be fetched from db or first time you will fetch it and then cache it. 其中themeName将是服务器端变量,您将在控制器中进行编程以从db中获取数据,或者第一次将其获取然后缓存它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM