Display Application version in Yesod

Posted on  by 

To display the version of your Yesod application is pretty simple. In this blog post you will learn the trick how to achieve this.

First, let's define our goal: We would like to show something like this in the default layout (probably footer):

  <div .version>Ver. #{appVersion}

To achieve this, we can use Cabals feature to create an auto generated Module called Paths_{projectName} Cabal docs on Paths module. This module as well exports the version of our application (defined in the Cabal file). To use this module we just add Paths_{appName} to our cabal file in the other-modules: section. The appName part is to be replaced by your application name, here are some examples:

  • App name "ersocon", this corresponds to the Paths_ersocon module
  • App name "ersocon-blog", this corresponds to the Pathsersoconblog module

With this integration of the auto generated module we can now add two imports to our Foundation.hs file:

import qualified Paths_ersocon
import Data.Version (showVersion)

The Paths_ersocon module will provide the version. With the Data.Version module we can transform our version into a Template friendly Text. In the defaultLayout function you can now add the following line:

defaultLayout widget = do
    -- other code (omitted in this blog post)

    let appVersion = showVersion Paths_ersocon.version

This way we can start using appVersion in out templates and our initial example will compile. From now on, each time you bump the version in your Cabal file the version in your template will change as well.