Use Twirl in your next project

The Twirl project is the engine used by Play to support the creation of views in our projects. A some time ago, this project was separated from the core of the framework and, nowadays, is possible to use this fantastic template engine in any Scala/Java project.

The only problem is that you have to use SBT in order to have your views being compiled all the time. Thinking about that, @csokol and I have created a new project that allows you to use Twirl in every app, using SBT or not. Basically we created a class which compiles your views and other class that watches every change in the views directory.

If you are using maven, you can use whatever you want, you just need to follow a simple steps. First you have to download our project, for while you need to clone the repo and install locally. After that, you just need to run our class :).

 mvn exec:java -Dexec.mainClass="" -  Dexec.cleanupDaemonThreads=false

Put some views in the source folder src/main/twirl/simple.scala.html, for example:

  @(customer: String)

   <h1>Welcome @customer</h1>

Let’s say you are writing a servlet and want to return some html for the browser. We just need to use the generated class!

   public class TwirlServlet extends HttpServlet {
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		Html clientPage = html.simple.apply("client");


Twirl is a powerful template engine and deserves some attention. The Java world needs a new approach to write views, JSP is too old and does not have a good support to a lot of things needed by our views. You already have Freemarker but, at least in my opinion, Twirl and its compiled views is the next step. For example, you can test your view in a simple way, just invoke a method :).

   public void shouldContainsCustomerName(){
      Html clientPage = html.simple.apply("client");

That is it! This was a different post, not completely related with Play, I hope you had enjoyed.

Reminders about deploying Play apps in Heroku

Put your application in production, also known as deploy, is one of the most important aspects of our lives as developers. This is one more thing that is too much easy with Play. Let’s walk through for some important details about deploying apps in Heroku.

There is step by step guide here. The main problem that I usually see with people that are trying to use heroku is about the details in the procfile and the database configuration. The procfile is used to configure heroku in order to start your application. This file is needed by Foreman, a ruby gem used to manage all processes necessary to run your application. Here an example:

  web: target/universal/stage/bin/acasaesuadecor-landing 

Heroku uses the stage command to generate the deploy structure for the Play app, that is why you used this path. You need to have this file in order to run your app. It also needs to be placed in the root of your project. All information between curly braces will be provided by Heroku, in their environment.

If you take a closer look, you will see the parameter named Ddb.default.url and this is a point of confusion between a lot of people. All the information needed to connect to the database is provided in the url. You only need to configure, in your production.conf, the driver information.


If you configure, for example, the password. This information will be used and you will get an exception in Heroku, explaining that the password is wrong.

That is it :). I am gonna try to write about the webjars in the next post!