Tuesday, January 29, 2013

I admit, it has been a while since I have done web development. To be more precise, the last framework I used was Struts 1.x (back in early 2000's). It was the de facto MVC framework of its time, but I end up loving it when I found tiles. Tiles introduced the template format, and is one reason why I end up using Play 2 along with Scala in my new web applications. The use of templates is something that I really enjoy - makes my job a lot easier and more productive.

Lets say that you have a series of products to display. Your Play controller will look at something like this:

Now, lets assume that the app/views/catalog.scala.html contains the following:
The code will show all the products, but also it shows the navigation, and footer. Clearly, we want to separate these contents so we can reuse them in another application. Furtheremore, in case that the footer has all the javascripts and Google analytics, we need to make sure that indeed all pages get tracked.

Using Play 2, you can simply extract all your navigation to a file name app/views/navigation.scala.html that will contain the navigation code:

Then, do the same thing with the fotter app/views/footer.scala.html:
Now, to show the contents of the navigation and the footer, simply use the code @navigation() and @footer() to show the contents. The catalog.scala.html will now be like this:

The other reason that I really enjoy Play 2 with Scala is the reverse routing. Reverse routing is a way to programmatically access the routes configuration, to generate a URL for a given action method invocation. In other words, you can do reverse routing by writing Scala code!

As you can see in the navigation, I don't have any hard coded routes. For example, home has the link: "@routes.Application.home". This is perfect in case of refactoring! My routes for home and catalog will depend on the configuration of my routes (contained in the conf directory):

If tomorrow I want to change the path, that will not affect my code at all, just the route file.

Again, this is an efficient way of programming because you can leverage the templates to build a user-interface view and you can use user-friendly URL with the help of your routes.

Monday, January 28, 2013

When I was learning Scala I stumbled into the implicit keyword. It took me a bit to understand it, but I really got into it once I started using Play. The best way that I can explain it is by thinking about "extending" the class without actually changing the code. Here are some examples:
This type of code is very useful. If you are using the Play framework, then you probably seem this type of code:
In here, we are implicitly extending the HTTP request so we can inject a product list.
This type of code is very useful because you can still have the Person class without been compromised (immutable/intact).
The same goes for the example below:
Using Anorm, we use implicit to use the SQL connection.
However, as you can imagine, this can also have some problems. You can read more about it here.