Play 2.5 - Storing Templates in a Database - java

I'm using Play 2.5 and there is a requirement to store all of the relevant static HTML Template data in a column in the database that we can then call and pass relevant object into.
This is proving difficult as it seems that Play requires the template to exist as static files included in the classpath prior to running.
For example, say I have an index.scala.html file that looks like this:
<html>
Hello #name!
</html>
I want to store this template in a variable (i.e. String template) that I can pass objects into like so:
String template = GreetingDTO.getTemplate();
Content html = template.render(User.getName());
This is essentially all I do to do regarding templating at this point. I need to format some passed in objects into a user-editable HTML layout for E-mail Notifications.
Is this possible without hacking around with Play's classpath structure? Where would I even begin with this? Could I possible achieve this easier by using a different template engine than Twirl. The only example I have found is this old Freemarker post from several years ago so was hoping there might be something a little more current.

You won't be able to use Twirl, the default template engine, because it is compiled to scala code and then to bytecodes and that all happens at compile/build time. From the docs:
Templates are compiled as standard Scala functions, following a simple naming convention. If you create a views/Application/index.scala.html template file, it will generate a views.html.Application.index class that has an apply() method.
Of course, it is possible to change the template engine and you can use another template engine that better fits your requirements (dynamically load and parse templates). Finally, take a look at the modules directory.

Related

what is the Proper use of the play template system

Got three questions on the play Scala template system.
Just a clarification:
I am assuming you create a new template for each kind of page that you want to produce - that correct? So, a log in page, an accounts page, home page, etc? Does this mean I should expect to have quite a few templates?
If I am say storing a history of orders - using java OO features with classes such as OrderLog and order. Would it be ok to call a java method in OrderLog.java that directly creates html text that I simply pass to the Scala template or is this a bad idea? Should I instead give the objects to the Scala engine and have that translate them?
The two considerations I am thinking of are whether the Scala is faster at rendering the text and whether it is just bad style to have the java compose the string.
How do I render JSP using the play framework?
For good design and ease of use, you'd probably want a modular design, where repeated structures (such as a footer for instance) have their own templates. Sometimes a large portion of multiple different pages could be reused, where you should only write it down once in a template that you use on all pages (can also parametrize your template to serve two almost similar pages). Thus, you should create plenty of templates. Think about them as methods in your code, helps being DRY (Don't Repeat Yourself) and KISS (Keep It Simple Stupid), not trying to solve all the problems in one template or forcing every page to have their own template that does practically same things as another page.
According to the MVC pattern, you should leave all rendering details for the views (in this case your templates) and keep it out of your models (OrderLog). Should be less verbose to do it in a template than trying to create HTML on server side and all your HTML is in one place, so it's clearer for new people - or yourself after a long break from working with this application.
You shouldn't use JSP, just the template engine. It can do everything plain JSP's would and more. Templates will be rendered when you have the server up and running and load the pages (on dev mode). You can keep changing POJO's or templates without having to republish the application or restart the server to see the differences.

nested templates using velocity

I have a small java web project which should create a few web pages.
All pages should have the same boilerplate (css, javascript, etc.) but indiviual content (i.e. the content of the main div.)
What is the usual way of implementing this kind of setup in velocity?
Currently the only way I see is rendering the boilerplate template which uses #parse to include the actual page which is given as a context parameter.
Is there an easier way?
I come from sinatra where you render the actual page and implicitly add the boilerplate around it. Do you know of any similar templating framework in Java?
In sinatra you could for example do:
get '/' do
haml :index, :layout => :master
end
to render the index page in the master layout. (Or by leaving out the layout part in the default layout.)
I'd recommend you use the VelocityLayoutServlet from the VelocityTools project: http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/view/VelocityLayoutServlet.html

Semi dynamic constants in GWT

For GWT we use static constants to provide internationalization for our users. However, this makes reviewing and editing the texts a tedious process, because if one of our stakeholders has comments, it has to be compiled and deployed to our demo environment again. The solution would be to have some kind of semi dynamic text constants.
What I would like, is that I can compile to some kind of "review mode", and when I do that, the constants are read from a file from the server or database. If possible I would like to be able to edit this file, so stakeholders can modify the texts themselves (using some kind of text edit widget I would have to write for that). Then we can develop, test and demo with these texts. If we are satisfied, we compile for production mode, which uses a old fashioned constants resource bundle, compiled completely in JavaScript.
Does somebody know if something like this exists, or have some pointers on how to implement this?
It is a very surprising situation that GWT programmers often overlook the usefulness of JSPs and the Dictionary class. Even though many of us had tons of JSP experience prior to using GWT.
Dictionary class
You can define your "static" information as javascript var objects in the html hosting file. The Dictionary class can be used to read those javascript objects any time after moduleload.
JSP
The HTML "hosting" file, i.e. the html file used for launching GWT need not be an HTML file. It can be a HTML file dynamically generated by a JSP.
If you are familiar with JSP, you could turn an HTML file into a JSP just by changing its extension. Now turn the javascript object section you used for defining GWT "static" information into being dynamically generated by the JSP.
Voila!
I use JSP as the hosting file when I need to generate user- or session- specific "static" information for the GWT client. The JSP could read from the database or from some conditionally chosen text files.

Simple way to apply template to existing HTML code

Suppose I have a bunch of (very simple) HTML pages, that I want to apply a common 'theme".
These files are downloaded using various Groovy scripts, and I would like to apply to them this styling during a maven build. How could I do that ?
Using which framework/library could I do that ?
Furthermore notice I want to do that in a static fashion, that's to say I want to have the following process to occur
Files are downloaded by Groovy scripts
They are processed (in a "magical" fashion) by this library
They may be sent by FTP/SCP to an hosting server
Do you know such an easy to use library ?
Depends on the details of the task but having in mind the steps you've described you can consider using velocity templates.
I would suggest using sitemesh decorator. I am a user of old version but a new release is being worked on that allows you to do exactly what you are asking for. Do a google search on sitemesh and you should find lots of examples.
In a nutshell sitemesh decoration: basic html + template = decorated page.

Is there something like JET (Java Emitter Templates) but without Eclipse-dependencies?

I wanted to use JET (Java Emitter Templates)
in my Netbeans projects, but had to find out that JET
heavily depends on Eclipse libraries.
Is there something similar to JET, but as a standalone project?
Something which is open source and well maintained?
Futhermore, is "code generation" the common term for such tools?
If you are using Maven, you can use JET templates with the maven-jet-plugin.
This seems to be the source code. And here the documentation.
It is not actively maintained but works pretty well and follows the JET spec.
I've used it with templates and skeletons.
It's self contained, doesn't depend on Eclipse, and doesn't introduce any transitive dependencies to your project.
Indeed JET is very tied with Eclipse.
If you want to generate code without Eclipse you should use another solution.
You can choose another template engine like Velocity (https://velocity.apache.org/) or FreeMarker (https://freemarker.apache.org/).
Or you can choose a code generator working by itself independently of any IDE.
For example "Telosys Command Line Interface" : http://www.telosys.org/
From what I know, JET is something like JSP, no?
Java Emitter Templates are very similar to Java Server Pages (JSPs). Both JETs and JSPs use the same syntax, and are compiled to Java behind the scenes. Both are used to separate the responsibility for rendering pages from the model and controller. Both accept objects passed into them as an input argument, both allow inserting string values within code ("expressions"), and allow direct use of Java code to perform loops, declare variable, or perform logical flows ("scriptlets"). Both are good ways of representing the structure of a generated object (web page, Java class, or file) while supporting customization of the details.
JETs differ from JSPs in a few key ways. In a JET, the structure of the markup may be changed to support generating code in different languages. Typically the input to a JET will be a configuration file and not user input (though there is nothing forbidding this). And also typically, JET processing will take place only once for a given workflow. These are not technical limitations, and you may find uses for JETs which are quite different...
ibm.com
Here are a few links to get you started on JSP, if that sounds like what you need:
sun.com
netbeans.org
Look for "template engine" for these types of tools.
A couple you might want to look at:
Apache Velocity (http://velocity.apache.org/)
StringTemplate (http://stringtemplate.org/)
I ended up using ERB (Ruby's template engine).
Works great in Netbeans!
I define custom ant task which generates source files by calling ERB (whose results are placed inside a non-versioned special directory).
The ant task is overriding Netbeans' "-pre-compile" task.

Categories