Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework runtime generation of templates

Using The Play Framework how easy would it be to allow an admin user to create and edit templates and then render the templates.

So essentially CMS like functionality.

I am new to the Play Framework and am evaluating it for use in a personal project.

Edit
An alternative is just to use an editor like CKEditor and just save the html from that in the database and provide some reskinning through CSS but would prefer to allow users who know more to edit templates as well.

like image 310
eaglestorm Avatar asked Nov 14 '22 08:11

eaglestorm


1 Answers

The answer to this question depends on if you need dynamic content in the editable pages.

If you don't, then just save HTML text. If you do, and it is really limited (like replacing a name field), then you could simply do a simple string substitution on the HTML text.

If you decide you want the full glory of editing Play templates on the fly (and are ok with the security consideration), then you can compile templates. Note this is how it is done in Play 1.2.x - though no doubt similar in Play 2.x. Make sure you cache templates, since they are expensive to compile (check TemplateLoader.load(VirtualFile file) for ideas on how to cache and update)

String name = "mytemplate";
String source = "hi ${name}";
Template template = new GroovyTemplateCompiler().compile(new GroovyTemplate(name, source)));
Map<String,Object> args = new HashMap<String,Object>();
args.put("name","mom");
String renderedHtml = template.render(args);
like image 164
Tom Carchrae Avatar answered Nov 16 '22 02:11

Tom Carchrae