简体   繁体   中英

Theming support in Play Framework 2.0

How can i implement theming support in Play Framework 2? I want to create directory structure like: views/default <- default template directory
views/site1 <- template for site 1
views/site2 <- template for site 2

If template doesn`t exist (ie. views/site1/home) it should render template from default directory.

I have tried cls = Class.forName("views.html.home); But I get class not found exception.

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. 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 .

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. 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">

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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