Migrating to Yesod 0.2

May 3, 2010

GravatarBy Michael Snoyman

I've nearly completed the migration of my largest site from the current version of Yesod to the upcoming Yesod 0.2 release. I just wanted to share some of my experience.

  • In this site, the code fairly evenly splits into three parts: database interaction, logic code (site parsing to be specific) and the web interface. As would be expected, the first two required no modification at all.
  • The most time was spent migrating from HStringTemplate to Hamlet, but I'm very happy with the results over all. Overall, I'd break it down like this:

    Advantages

    • Easier to follow nesting.
    • Less typing.
    • Nicer to declare a new datatype than to create arbitrary nested-string-like data structures.
    • Type-safe URLs are a huge win.
    • When I'm lazy in a conversion, I can just copy in HTML code, and it usually works.

    Disadvantages

    • There's not good way to conditionally add a attribute. Most of the time, that's no big deal; but when you want to add "selected" to an option tag, it's an annoyance. I ended up writing code like this:
      <option foo=bar
      $if shouldBeSelected
         selected
      ></option>
      This is clearly sub-optimal.
    • You can have multi-word values for attributes embedded in the template. So in order to achieve <input type="submit" value="Click me">, I ended up writing %input!type=submit!value=clickMe, and declaring a function clickMe _ = cs "Click me".
    • It can be a little tedious having all those extra datatypes just for template arguments, but I suppose that's the price to pay for type safety.
  • I much prefer the new route syntax. Previously, it used Yaml; that was really too verbose. Now each resource is defined on a single line, and handler names are automatically determined from the constructor name.
  • Type-safe URLs already caught a few bugs for me. I think this will be a major cornerstone for Yesod programming. I can now easily write code such as redirect RedirectTemporary $ UserPage userId, and it just works.
  • Subsites are great, but it's a very complicated concept. It's made web-routes-quasi more complex than I would have hoped to, and necesitated added some extra information to the Handler monad. I think that this will pay off in the end; unfortunately, Yesod 0.2 subsites may still be a little unpolished.

For those of you playing around with Hamlet, I would definitely point out: do not be afraid to define helper functions that ignore their first argument. It's been completely necessary for me.

I'm still adding tutorials, refining the documentation and making some code changes, but I should be releasing very soon (latest would be next week). I encourage everyone to check out the documentation and give me any feedback you have! I'm especially interested in people's thoughts on a persistance layer, which is notably lacking right now.

Comments

comments powered by Disqus

Archives