FreeMarker vs. Velocity

Discussions

News: FreeMarker vs. Velocity

  1. FreeMarker vs. Velocity (11 messages)

    Matt Raible has been working with Spring and Velocity. He recently tried to bootstrap FreeMarker and has learned a few lessons about the templating language, which he describes to the community.

    Read FreeMarker vs. Velocity

    Threaded Messages (11)

  2. FreeMarker vs. Velocity[ Go to top ]

    Matt,

    As I can't find the entry on your blog, I'll simply reply here:

    Both VelocityView and FreeMarkerView offer a "requestContextAttribute" property where you can specify the name for an instance of Spring's RequestContext class, to be exposed in the template model. Among other things, this allows easy - and consistent - access to the request URI, without exposing the HttpServletRequest itself to the template.

    Juergen
  3. FreeMarker vs. Velocity[ Go to top ]

    Blog entry is on:
    http://jroller.com/page/raible?anchor=freemarker_vs_velocity
  4. FreeMarker vs. Velocity[ Go to top ]

    Both VelocityView and FreeMarkerView offer a "requestContextAttribute" property where you can specify the name for an instance of Spring's RequestContext class, to be exposed in the template model. Among other things, this allows easy - and consistent - access to the request URI, without exposing the HttpServletRequest itself to the template.
    Right, my inability to access the request itself seems to be related to SiteMesh's Decorators. The VelocityDecoratorServlet seems to expose the HttpServletRequest object because ${request.requestURL} works, but the FreemarkerDecoratorServlet seems to only exposes the request attributes, not the request object to call methods on.

    As for the
    RequestContext it doesn't seem to have a getRequestURI() or a getRequestURL() method. Noth that they're needed, getContextPath() is usually all that's needed.
  5. The blog article says:
    "In the process, I found out that FreeMarker is a bit more strict about the whole MVC thing. With Velocity and JSP, I can remove session variables using $request.session.removeAttribute("varName") and <c:remove var="varName"/>."

    Let me correct this. FreeMarker is actually less rigid about MVC, since it has more programming capabilities than Velocity has (e.g. local variables in macros, breaking out from loops, etc.). And it cleanly doesn't prevent you from removing attributes... you can even format the HDD with a FreeMarker template, it's just matter of what object you expose to the template and with what object-wrapper. So if you can't do it in Spring, then that's the characteristic of the Spring FreeMarker Decorator (which is not part of the FreeMarker project), and surely not of FreeMarker.
  6. FreeMarker vs. Velocity[ Go to top ]

    As for availability of request and session objects - you can alway bind the HttpServletRequest object as an attribute of itself (in a global servlet filter), or the HttpSession object as an attribute of itself (using a HttpSessionListener) if you want them accessible - your choice. FreeMarker doesn't prevent you from doing that, but doesn't encourage it either. It only allows it :-)

    As for empowering - JSP 2.0 indeed improved on lots of things; with these improvements it is undeniably catching up with FreeMarker's power - the power we've been providing to our users even back in days when JSP 2.0 was only wishful thinking. I.e. in JSP 2.0, you can finally define reusable snippets in JSP itself, instead of having to resort to coding a custom tag in Java that only spits out HTML whenever you wanted a reusable snippet of HTML code. However compared to FreeMarker's (or Velocity's) macros, the deployment scheme is a bit awkward - you have to deploy each JSP "macro" in a separate file AFAIK (I might be wrong...) FreeMarker's macros can OTOH do all sorts of nice things - take other macros as arguments to execute them as part of their own execution, have default values for arguments, etc.

    FreeMarker's clean concept of macro libraries with namespaces for avoiding name conflicts is still not beaten, IMHO. We also have a very nice syntax for specifying list and map literals that can often come in handy while coding a template for increased productivity. I.e. I remember myself using construct like this to default to empty sequence when a variable that's supposed to contain the sequence happens to be undefined:

    <#foreach item in list?default([])>
    ...
    </#foreach>

    Here, both the FreeMarker's defaulting expression and the literal for empty list are used - simple, effective, and I'd say expressive too.

    Also, if you happen to use Jython as your language for business logic (not unrealistic - business rules change often and it's easier to deploy a modified script than a modified .class file) you will be able to use your Jython objects in a template directly - calling methods, accessing items etc. with their natural syntax. (Mind you, you can write "wrappers" for any other object model as well - we just happen to have one for JavaBeans and Jython shipped by default, but if anyone ever comes up with a need (and an implementation) of a wrapper for, say, Netscape Rhino JavaScript interpreter objects, we'll integrate that too...)

    And JSTL or not, JSP control structures are still overly verbose. <c.foreach...> still looks much uglier than <#foreach...> but I guess this really is just a matter of taste and not really a strong argument :-)

    My belief is that the only thing FreeMarker doesn't have that JSP 2.0 does is the "this-is-our-preferred-technology" label (actually, they not quite precisely call that "standards") from Sun :-)

    Peace,
      Attila.

    http://www.szegedi.org
  7. FreeMarker vs. Velocity[ Go to top ]

    [quote]The one thing I really like about FreeMarker over Velocity is the ability to use JSP Tags in your template. To me, this is very powerful since displaytag and struts menu are my primary reasons for sticking with JSP. [/quote]

    I'd highlight the another distinctive feature that may help put choice on FreeMarker - its ability to support XML as data model and use XPath in data fetching expessions.

    <#list root("./data/item-list/air-availability-item") as item>
      ...
      seat type: ${ item("./flight-seat/ticket-type")._text }
    </#list>

    --
    Mike Skorik
    http://www.100kSolutions.com
  8. templating question[ Go to top ]

    has anyone played w/ the idea of templating across multiple web applications? i'm new to the idea of templating, and i'm having trouble figuring out if it is possible to use technologies like velocity/freemarker to create a consistent user interface that can be used by multiple applications.

    if these tools can't solve my needs, are there other options? would a portal server help or not? thanks in advance.
  9. Tapestry?[ Go to top ]

    Matt, do you have any plans to try Tapestry next? Given Tapestry's growing popularity, and it's unique approach to MVC (components, events, etc), I think the community would learn a lot by seeing how it works out in your myUsers app.
  10. RE: Tapestry?[ Go to top ]

    Yep, I plan to do a Tapestry version of MyUsers in Chapter 13 of Spring Live. I'm actually working on it right now (followed by a JSF version) - so the code will be done long before the writing.
  11. RE: Tapestry?[ Go to top ]

    I have to say that velocity doesn't look too good compared to Freemarker. I've been using velocity on all of my projects as of late and for one very good reason - it's fast. I don't have to wait and sit around for compile times and all that. I can get immediate results and that's something every programmer who is mucking around with the view wants. I can also make macros in about a 20th the speed as making a jsp tag too. Granted I think page views are just naturally not supported very well at all in any programming language at the moment, but this is a step forward.

    However, I've been doing some pretty complex stuff in the view. Now, I don't mean I'm putting business logic in there - that's not it. I've just been making massive amounts of investment in macro libraries and I build higher-level marcos for all sorts of application-specific presentation reuse. However, Velocity just isn't any good at doing this - and I'm not even talking about large scale applications, I'm talking about a small to medium-sized but featureful project a competent developer can write in a few weeks.

    I think I've hit the capabilities of Velocity and I've been stretching it quite a bit. Without named/optional parameters or even basic macro overloading, I just can't build complex views and avoid duplication at the same time very easily. It's like a pain in the ass just to add an option column, button or sub-screen for a specific listing that uses the general listing macro and so on. I have all kinds of cases where I have to do functional-oriented type checking and it's inexcusable.

    Freemarker seems like the way to go. While it's probably more difficult, the end result looks to be more like html. When I saw features for unordered named, optional parameters and nested content, I realized that these features alone make it better than Velocity because they just aren't "nice" features, the are just down-right required.
  12. Date problem[ Go to top ]

    Hi All,

    I'm trying to format date & display it in the velocity template.I have coded as

    ${date.format('dd/MM/yy', $!pv.effectiveDate)}

    Now with this the date is being displayed in the given format.I want to code such that the date is displayed according to the area's date format....there is something like getLocale().I don't know how to use it in the velocity template.I'm absolutely new to velocity so any help is much appreciated.