views:

877

answers:

6

Over the past year I have heard alot about Velocity and NVelocity. Reading their documentation and doing searches on the net hasn't given me the answers I was looking for.

In what situation would I use this library in my development? What problem does it solve that didn't already have a solution?

+3  A: 

It's a template engine. If you have a lot of static text with variable content mixed in, templates are a great way to reduce the amount of work you have to do. It's a whole lot better than String.Format or loads of concatenation because it's not as repetitive or error prone, and far more maintainable since you can figure out exactly what your template does just by looking at it.

Rob Rodi
+6  A: 

Since the dawn of web apps, people started to think about separation of concerns in many applications, including web applications. The challenge is to separate what is view code from what is business code, or logic code. When jsps first arrived, many people where coding lots of logic in jsps directly (stuff like db access and other), breaking the basic principle of separation of concerns (jsps should be responsible for the presentation, not for the logic).

Velocity, Freemarker and others are templating engines that allows the separation of UI logic and business logic, thus facilitating changes in the presentation while minimizing the changes on the business side. These template engines have facilities for doing common UI tasks such as showing a block of html if some condition holds true, or iterating over a list while maintaining logic code outside of the view. This is fundamental for maintaining a complex application in the long run.

Miguel Ping
So this is different than say, using <% ... %> <%= .... %> in Rails, Asp.Net-Mvc, etc ?Does XSLT qualify as "UI" logic?
Bill
One way to judge the differences is if you were to have a designer to do the layout, how well does he/she would fare. Does a designer need to learn ruby/java/etc in order to change the UI? I guess he shouldn't need to learn it. Hey may need to learn new constructs such as taglibs, though.
Miguel Ping
As for the XSLT, I guess it depends on your app. If you are showing some sort of XML, I guess XSLT is UI logic only, since you typically don't do any other logic besides XML handling.
Miguel Ping
A: 

We use templating to generate configuration files for production, UAT, system test, contingency systems etc.

We have a master Spring configuration file into which we inject a property file. We have a master property file that is parsed by Velocity and this allows us to keep all system settings in one file.

Fortyrunner
+2  A: 

I think it's important to point out that compared to JSP/ASP.NET as a templating mechanisim, Velocity/NVelocity really 'ENFORCE' the seperation of concern.

With <% .. %> of JSP/ASP.NET, any Java/.NET code is allowed. Which is why sometimes you do see business logic code in these files.

With Velocity/NVelocity you can't embed long series of code. Instead you are really forced to pass in computed values, which Velocity/NVelocity picks up and displays them according to the way the template is designed.

Another point would be that they can work outside of a Web Container environment (at least Velocity can AFAIK). Imagine that you had designed a report template with JSP/ASP.NET. It works fine from the web. And then suddenly there is a change request to have it be done from a Desktop application. Rather than embed a Web Container in it, you could initialize Velocity/NVelocity, compute the values, then render the template.

Kent Lai
A: 

As a bonus, for the ones interested, I recommend reading the following:

These are links about StringTemplate, a templating engine by Terence Parr who wrote antlr, a parser that's been used everywhere (ex: hibernate uses antlr).

Miguel Ping
A: 

Yeah, but can someone answer me this:

why use apache velocity instead of pure JSP EL and includes?

It's been awhile... I've used both of those, and would pick EL if I had to. But lately I've been using Freemarker and it's been much better. Not necessarily the best, but... Velocity required a lot of contortions, for lack of a better word.
Rodney Gitzel
true, it's been awhile. well, so you're telling me that if I'm starting a new project today, I would not find any benefits of using velocity instead of just ELs and includes?