Yesod web framework/Printable version

Yesod web framework

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at

Permission is granted to copy, distribute, and/or modify this document under the terms of the Creative Commons Attribution-ShareAlike 3.0 License.


Server interface


Yesod uses a Web application interface API,[1] abbreviated WAI, to isolate servlets, aka web apps., from servers, with handlers for the server protocols CGI,[2] FastCGI,[3] SCGI,[4] Warp,[5] Launch (open as local URL to the default browser, closing the server when the window is closed),[6]

The foundation type


See ref.[7] Yesod requires a data type that instantiates the controller classes. This is called the foundation type. In the example below, it is named "MyApp".

The REST model identifies a web resource with a web path. Here REST resources are given names with an R suffix (like "HomeR") and are listed in a parseRoutes site map description template. From this list, route names and dispatch handler names are derived.

Yesod makes use of Template Haskell metaprogramming to generate code from templates at compile time, assuring that the names in the templates match and everything typechecks (e.g. web resource names and handler names).

By inserting a mkYesod call, this will call Template Haskell primitives to generate the code[8] corresponding to the route type members, and the instances of the dispatch controller classes as to dispatch GET calls to route HomeR to a routine named composing them both as "getHomeR", expecting an existing handler that matches the name.

Hello World


"Hello world" example based on a CGI server interface (The actual handler types have changed, but the philosophy remains):

{- file wai-cgi-hello.hs -}
{-# LANGUAGE PackageImports, TypeFamilies, QuasiQuotes, MultiParamTypeClasses,
             TemplateHaskell, OverloadedStrings #-}
import "wai" Network.Wai
import "wai-extra" Network.Wai.Handler.CGI (run) -- interchangeable WAI handler

import "yesod" Yesod
import "yesod-core" Yesod.Handler (getRequest)
import "text" Data.Text (Text)
import "shakespeare" Text.Cassius (Color(..), colorBlack)

-- the Foundation type
data MyApp = MyApp

-- sitemap template, listing path, resource name and methods accepted
-- `mkYesod` takes the foundation type name as param. for name composition of dispatch functions
mkYesod "MyApp" [parseRoutes|
/ HomeR GET

instance Yesod MyApp

-- indentation structured CSS template
myStyle :: [Text]  CssUrl url
myStyle paramStyle =
    border: 1px solid #{boxColor}
          boxColor = case paramStyle of
                        ["high-contrast"]  colorBlack
                        _  Color 0 0 255

-- indentation structured HTML template
myHtml :: [(Text, Text)]  HtmlUrl url
myHtml params = [hamlet|
<!-- indentation, or lack of it, under starting tags or commands ('$' prefix) 
     describe the content or sequence tree structure -->
<!-- '.' or '#' prefixes in tags introduce css styled "class" or "id" attribute values -->
<!-- interpolation of haskell expressions follow the "shakespeare templates" #{expr} syntax -->

<p>Hello World! There are <span .box>#{length params} parameters</span>:
$if null params
    <p>Nothing to list 
         $forall param <- params
             <li>#{fst param}: #{snd param}
getHomeR :: Handler RepHtml
getHomeR = do
        req <- getRequest
        let params = reqGetParams req
        paramStyle <- lookupGetParams "style"
        defaultLayout $ do
            -- adding widgets to the Widget monad (a ''Writer'' monad)
            setTitle "Yesod example"
            toWidgetHead $ myStyle paramStyle
            toWidgetBody $ myHtml params

-- there are ''run'' function variants for different WAI handlers

main = toWaiApp MyApp >>= run
# cgi test
export PATH_INFO=/
export QUERY_STRING='p1=abc;p2=def;style=high-contrast'


Resources, routes and HTTP method handlers


See ref.[9][10] Yesod follows the REpresentational State Transfer model of access to web documents, identifying docs. and directories as resources with a Route constructor, named with an uppercase R suffix (for example, HomeR).

The routes table
The parseRoutes template should list the resources specifying route pieces, resource name and dispatch methods to be accepted.

URL segment capture as parameter is possible specifying a '#' prefix for single segment capture or '*' for multisegment capture, followed by the parameter type.

-- given a MyApp foundation type

mkYesod "MyApp" [parseRoutes|
/                     HomeR      -- no http methods stated: all methods accepted
/blog                 BlogR      GET POST

-- the '#' prefix specify the path segment as a route handler parameter
/article/#ArticleId   ArticleR   GET PUT

-- the '*' prefix specify the parameter as a sequence of path pieces
/branch/*Texts        BranchR    GET

-- to simplify the grammar, compound types must use an alias, eg. type Texts for ''[Text]''
  • Applying the previous template generates the following route constructors:
data Route MyApp = 
    HomeR                    -- referenced in templates as: @{HomeR}
    | BlogR                  -- in templates: @{BlogR}
    | ArticleR ArticleId     -- in templates: @{ArticleR myArticleId}
    | BranchR Texts          -- in templates: @{BranchR myBranchSegments}
  • For every supported HTTP method a handler function must be created to match the dispatch names generated by mkYesod from the parseRoutes template, by prefixing the method name (or the prefix "handler" if no method stated) to the resource, as described (actual versions handler types have changed, but the philosophy remains):
-- for "/ HomeR"        -- no http methods stated ⇒ only one handler with prefix ''handler''
handlerHomeR :: HasReps t  Handler t

-- for "/blog BlogR GET POST"
getBlogR :: HasReps t  Handler t
postBlogR :: HasReps t  Handler t

-- for "/article/#ArticleId ArticleR GET PUT"
getArticleR :: HasReps t  ArticleId  Handler t
putArticleR :: HasReps t  ArticleId  Handler t

Request data, Parameters, Cookies, Languages and other Header info


See ref.[9]

Authentication and authorization


See ref.[11] Authentication plugins: OpenId, BrowserId, Email, GoogleEmail, HashDB, RpxNow.[12]

There is an important setting for automatic redirection after authentication.[13]



See ref.[14] Session back-ends: ClientSession[15] (it stores the session in a cookie), ServerSession[16][17] (it stores most of the session data at the server)

>> To avoid undue bandwidth overhead, production sites can serve their static content from a separate domain name to avoid the overhead of transmitting the session cookie for each request

Session messages


A success, failure or indicative message can be stored (setMessage) in the Session and will be shown, if it exists, by the default_layout routine through the default_layout.hamlet template, being cleared on consultation.[18]



Common URL prefix subsites for workflows, file serving or site partitioning. See ref.[19][20]

Built-in subsites: Static,[21][22] Auth[23]

Form processing and layout generation


See ref.[24]

The Form type here is an object that is used in the controller to parse and process the form fields user input and produce a (FormResult, Widget) pair were the widget holds the layout of the next rendering of the form with error messages and marks. It can also be used to generate a new form with blanks or default values.

The form type takes the shape of a function of an html snippet to be embedded in the view, that will hold security purpose hidden fields.

A form object is generated from an Applicative/Monadic composition of fields for a combined/sequential parsing of field inputs.

There are three types of forms:

  • Applicative (with tabular layout),
  • Monadic (with free layout style), both in the Yesod.Form.Functions module,
  • Input (for parsing only, no view generated) in the Yesod.Form.Input module.

The field generators, whose names are composed by the form type initial (a|m|i) followed by (req|opt){- required or optional -}, have a fieldParse component and a fieldView one.[25]

  • the function runForm{Post|Get} runs the field parsers against the form field inputs and generates a (FormResult, Widget) pair from the views offering a new form widget with the received form field values as defaults. The function suffix is the http method used in the form submission.
  • while generateForm{Post|Get} ignores inputs from the client and generates a blank or defaults form widget.[26]

The actual function parameters and types have changed through Yesod versions. Check the Yesod book and libraries signatures.

The magic is in the FormResult data type Applicative instance, where (<*>) collects the error messages for the case of FormFailure [textErrMsg] result values[27]

Monadic forms permit free form layout and better treatment of hiddenField members.[24]

A sample of an Applicative[28] form:

-- a record for our form fields
data Person = Person {personName :: Text, personAge :: Int, personLikings :: Maybe Text}

-- the Form type has an extra parameter for an html snippet to be embedded, containing a CSRF token hidden field for security
type Form sub master x = Html  MForm sub master (FormResult x, Widget)

-- for messages in validation functions:
  @param master: yesod instance to use in renderMessage (return from handler's getYesod)
  @param languages: page languages to use in renderMessage

-- optional defaults record:
  @param mbPersonDefaults: Just defaults_record, or Nothing for blank form

personForm :: MyFoundationType  [Text]  Maybe Person  Form sub master Person
{- ''aopt'' (optional field AForm component) for "Maybe" fields,
   ''areq'' (required fld AForm comp.) will insert the "required" attribute
personForm master languages mbPersonDefaults = renderTable $ 
  Person <$> areq textField            fldSettingsName    mbNameDefault 
         <*> areq customPersonAgeField fldSettingsAge     mbAgeDefault 
         <*> aopt textareaField        fldSettingsLikings mbLikingsDefault 
    mbNameDefault    = fmap personName    mbPersonDefaults
    mbAgeDefault     = fmap personAge     mbPersonDefaults
    mbLikingsDefault = fmap personLikings mbPersonDefaults

    -- "fieldSettingsLabel" returns an initial fieldSettings record
    -- recently the "FieldSettings" record can be defined from a String label since it implements IsString
    fldSettingsName = (fieldSettingsLabel MsgName) {fsAttrs = [("maxlength","20")]}
    fldSettingsAge  = fieldSettingsLabel MsgAge
    fldSettingsLikings = (fieldSettingsLabel MsgLikings) {fsAttrs = [("cols","40"),("rows","10")]}

    customPersonAgeField = check validateAge intField

    validateAge y
        | y < 18    = Left $ renderMessage master languages MsgUnderAge
        | otherwise = Right y


  1. "The wai package". Retrieved 2012-10-23.
  2. "The wai-extra package with CGI WAI handler". Retrieved 2012-10-23.
  3. "The wai-handler-fastcgi package". Retrieved 2012-10-23.
  4. "The wai-handler-scgi package". Retrieved 2012-10-23.
  5. "The warp package". Retrieved 2012-10-23.
  6. "The wai-handler-launch package". Retrieved 2012-10-23.
  7. a b "book - Basics". Retrieved 2012-10-23.
  8. The mkYesod code
  9. a b "book - Routing and Handlers". Retrieved 2012-10-23.
  10. "Playing with Routes and Links". 2012-10-17. Retrieved 2012-10-28.
  11. "book - Authentication and Authorization". Retrieved 2012-10-23.
  12. "The yesod-auth package". Retrieved 2012-10-26.
  13. "book - Sessions - See section "Ultimate Destination"". Retrieved 2012-11-17.
  14. "Sessions". Retrieved 2012-10-23.
  15. "Web.ClientSession". Retrieved 2012-10-25.
  16. "ServerSession: secure modular server-side sessions". Retrieved 2018-10-29.
  17. "Web.ServerSession.Frontend.Yesod". Retrieved 2018-10-29.
  18. "Session Messages". Retrieved 2018-10-23.
  19. "Creating a Subsite". Retrieved 2012-10-25.
  20. "Yesod and subsites: a no-brainer". 2012-08-22. Retrieved 2012-10-28.[]
  21. "The Magic of Yesod, part 2 - See section "Static Subsite"". 2010-12-25. Retrieved 2012-10-25.
  22. "The package yesod-static - Static Subsite". Retrieved 2012-10-25.
  23. "The package yesod-auth - Auth Subsite". Retrieved 2012-10-25.
  24. a b "book - Forms". Retrieved 2012-10-23.
  25. "Yesod.Form.Fields". Retrieved 2012-10-23.
  26. "Yesod.Form.Functions runFormPost". Retrieved 2012-10-25.
  27. "Yesod.Form.Types". Retrieved 2012-10-23.
  28. "HaskellWiki - Applicative functor". Retrieved 2012-10-24.




The Handler monad returns content in one or more of several formats as components of types that implement the HasReps class[1] {RepHtml, RepJson, RepXml, RepPlain, the dual RepHtmlJson, a pair or list of pairs [(ContentType, Content)], ..}.[2][3] Json examples:[4][5][6]

The HasReps default implementation of chooseRep chooses the document representation to be returned according to the preferred content-type list of the client accept header.[1]

Widgets[7] are HTML DOM code snippets made by specific commands (e.g. setTitle) or from templates of structure (html) / behaviour (javascript) / style (css), whose types instantiate the classes ToWidget, ToWidgetHead or ToWidgetBody.

A Widget monad,[8] based on a Writer[9] one and argument to defaultLayout, facilitate to piece the widgets together.

Template interpolation - Shakespearean templates


See ref.[10] These are content view templates that follow a common substitution pattern of code expressions within curly brackets with different character prefix to refer to

template expressions with ^{...}
other templates of the same type as ^{template params},
route expressions with @{...}
safe (typed) urls as @{HomeR},
message expressions with _{...}
i18n message rendering as _{MsgMessage params}
other Haskell expressions with #{...}
haskell expression rendering as #{haskell_expression} which type must be convertible
    • in case of hamlet html templates, the expression type must be an instance of Text.Blaze.ToMarkup[11]
    • in case of css templates, the expression type must be an instance of Text.Cassius.ToCss[12]
    • in case of javascript templates, the expression type must be an instance of Text.Julius.ToJavascript [13]
    • in case of i18n message definitions (in "isoLang.msg" files) with parameter interpolations, the expression type must be an instance of Text.Shakespeare.I18N.ToMessage [14]
    • in case of plain text templates, the expression type must be an instance of Text.Shakespeare.Text.ToText [15]

Using non-English text in expressions requires use of the Unicode-aware type Text, since GHC's show for the type String renders non-ASCII characters as escaped numerical codes.

  • external file templates: Template content can be loaded from external files using compile time splice calls as $(expr).[16]
  • reload mode for external files: See doc.[10]
Localizable (i18n) messages

See ref.[17] For every supported language ISO name there should be a file in the messages subfolder as <iso-language>.msg with entries like

ArticleUnexistant param@Int64: unexistant article #{param}

For each entry in en.msg a message constructor is generated, prefixing the message name by "Msg", so the example msg. can be referred as

-- in code
myMsg = MsgArticleUnexistant myArticleId 
-- in templates
  _{MsgArticleUnexistant myArticleId}

Actual i18n support is missing from the stack app template. You have to add the mkMessage "MyApp" directory isoLangDefault to the "Foundation.hs" file to get the messages instantiated.[18]

HTML-like templates
  • the hamlet quasiquoter (a parser to compile-time Template Haskell code)[19][20] specified in the T.H. Oxford brackets syntax [qq| ... |] introduces an indentation based structured html template with '$' prefixed lines of logic statements (See doc.[10]).[21] Automatic closing tags are generated only for the tag at line start position.
  • the whamlet quasiquoter returns a Widget expression. (saves toWidget before [hamlet|..|]).
toWidget [hamlet|
$doctype 5
        <title>#{pageTitle} - My Site
        <link rel=stylesheet href=@{Stylesheet_route}>
          <p><span style="font-weight:bold;">_{MsgArticleListTitle}</span>
          $if null articles
                $forall art <- articles
                    <li>#{articleNumber art} .- #{articleTitle art}
JavaScript templates
  • the julius quasiquoter: introduces a javascript template.[22] Javascript variants CoffeeScript and Roy-language[23] have also specific quasiquoters.[19][22]
toWidgetHead [julius|

var myfunc = function(){document.location = "@{SomeRouteR}";}

CSS-like templates
  • the cassius quasiquoter: introduces a css template with indentation based structuring.[24]
toWidget [cassius|
    border: 1px solid #{myColor}
    background-image: url(@{MyImageR})

  • the lucius quasiquoter: introduces a css template with standard syntax plus shakespeare-template style substitutions.[25]
toWidgetHead [lucius|
.box { border: 1px solid #{myColor} ;
       background-image: url(@{MyImageR}) ;

Plain text templates
  • for e-mail or text/plain http content type.[26]
  1. templates: lt: lazy text, st: strict text
  2. templates for text with a left margin delimiter '|': lbt (lazy), sbt (strict)
[lt| Mr./Mrs. #{fullName} ... 

Specific views

  • Search engines XML Sitemaps,[27] where sitemap returns an XML Sitemap as http response, with the routes we want the search engines to crawl, and attributes to instruct the crawler, from a provided list of SitemapUrl records.
  • Navigation Breadcrumbs.[28] You have to provide a YesodBreadcrumbs instance for the site where the generator function breadcrumb should return a title and parent route for each one. Then, the query function breadcrumbs will return the present route title and ancestors' (route, title) pairs.
  • Web feed views (RSS / Atom).[29] You have handlers that return RepRss, RepAtom, or dual RepAtomRss content (to be selected on accept headers' preferred content-type list) from a given Feed structure.


  1. a b "The class HasReps". Retrieved 2012-10-23.
  2. "RESTful Content". Retrieved 2012-10-23.
  3. "The class ToContent". Retrieved 2012-10-23.
  4. "More Client Side Yesod: todo sample". 2012-04-23. Retrieved 2012-10-23.
  5. "JSON Web Service". Retrieved 2012-10-23.
  6. "The yesod-json package". Retrieved 2012-10-23.
  7. "book - Widgets". Retrieved 2012-10-23.
  8. "The widget monad". Retrieved 2012-10-23.
  9. "The Writer monad". Retrieved 2012-10-23.
  10. a b c "book - Shakesperean templates". Retrieved 2012-10-23.
  11. "Class Text.Blaze.ToMarkup". Retrieved 2012-10-23.
  12. "Class Text.Cassius.ToCss". Retrieved 2012-10-23.
  13. "Class Text.Julius.ToJavascript". Retrieved 2012-10-23.
  14. "Class Text.Shakespeare.I18N.ToMessage". Retrieved 2012-10-24.
  15. "Class Text.Shakespeare.Text.ToText". Retrieved 2012-10-24.
  16. "Template Haskell". Retrieved 2012-11-03.
  17. "book - Internationalization". Retrieved 2012-10-23.
  18. mkMessage
  19. a b "HaskellWiki - QuasiQuotation". 2012-05-26. Retrieved 2012-10-23.
  20. "Template Haskell Quasi-quotation". Retrieved 2012-11-02.
  21. "The hamlet template module". Retrieved 2012-10-23.
  22. a b "The Julius template module". Retrieved 2012-10-23.
  23. "Roy language". Retrieved 2012-10-23.
  24. "The Cassius template module". Retrieved 2012-10-23.
  25. "The Lucius template module". Retrieved 2012-10-23.
  26. "Shakespeare plain text templates module". Retrieved 2012-10-24.
  27. "The yesod-sitemap package". Retrieved 2012-10-26.
  28. "The YesodBreadcrumbs class". Retrieved 2012-11-05.
  29. "The yesod-newsfeed package for RSS / Atom views". Retrieved 2012-10-26.




Using in-memory mutable data (in the foundation datatype)


E.g. a visitor count. See ref.[1]

The Database layer

  • persistent is the name of the database access layer with templates for generating types for entities and keys as well as schema initialization.[2][3][4]

There is first class support for PostgreSQL, SQLite, MongoDB, CouchDB and MySQL, with experimental support for Redis.[2]

The Database layout is described in a template listing the entities, fields and constraints.[5]

  • For every entity listed, an integer key column "id" is generated with autoincrement and primary index attributes, with a type alias appending Id to the entity name
  • For every entity listed, a record type named as the entity is generated were record fields names are composed prefixing the entity name to the field name like "personName". An EntityField type "PersonName" is also generated for foreign key referencing from other entities.
  • There is an automatic database schema migration mechanism for DB schema updates, which, to succeed, requires, when adding columns to existent tables, to specify Default-column-value constraints with sql level notation.[6]
  • "At most one" cardinality has a special mechanism around the type Checkmark.[7]
  • Weak entities (childs in life constrained owner-child relationships) have no special support for cascade delete triggers, but there are functions to deleteCascade manually in the Database.Persist.Class module.[8]
automatic table creation, schema update and table migration
Modifications of the entities template produces an schema update with automatic table creation, and migration for the DBMS's that support "ALTER TABLE" SQL commands in a migrateAll procedure, generated from the template content. See "Migrations" in ref.[2] to look for migration aware DBMS.
share [mkPersist sqlSettings,
       mkMigrate "migrateAll"   -- generates the migration procedure with the specified name
       ] [persist|

User   -- table name and entity record type
    -- implicit autoincrement column "id" as primary key, typed UserId
    ident Text             -- refers to db. table column "ident"; 
                     -- generates a record field prefixing the table name as  "userIdent"
    password Text Maybe         -- Maybe indicates Nullable field
    UniqueUser ident            -- unique constraint with space sep. field sequence

Email  -- table name and entity record type
    -- implicit autoincrement column "id" as primary key, typed EmailId
    email Text
    user UserId                 -- foreign key by specifying other tables EntityField types
    verkey Text Maybe

    newlyAddedColumn Text "default='sometext'::character varying"  -- sql level Default constraint

    UniqueEmail email     -- unique constraint
  • Esqueleto: is a haskell combinators layer to generate correct relational queries to persistent.[9]

Example for persistent rawSQL and Esqueleto queries.[10]



The following packages are part of the yesod-platform:[11]

  • email-validate: Validating an email address.[12]
  • mime-mail: Compose and send MIME email messages.[13]


  • Useful glue functions between the fb library and Yesod.[14]

Development cycle


New Yesod apps are generated from the HaskellStack tool[15] templates, replacing previous command "yesod init"

Stack based app. template names are prefixed by yesod as "yesod-{minimal | postgres | sqlite | mysql | mongo | ...}"

  • Since HaskellStack uses the stackage repo by default, extra packages from the hackage repo should be referred in the "stack.yaml" extra-deps section.
  • You may customize packages to a local subfolder. They must be referred in the "stack.yaml" packages section.

The "Yesod helper" tool

  • The yesod helper tool [16]
    • yesod devel run from the project site, recompiles and restarts the project at every file tree modification.
    • yesod add-handler adds a new handler and module to the project, adding an import clause for the handler in the "Application" module.

Deploying with Keter: A web app server monitor and reverse proxy server


See refs.[17][18] [19]

Keter is a process as a service that handles deployment and restart of Yesod web app servers, and, per web app, database creation for PostgreSQL.

The console command yesod keter packs the web app. as a keter bundle for uploading to a keter folder named "incoming".

Keter monitors the "incoming" folder and unpacks the app. to a temporary one, then assigns the web app a port to listen to, and starts it.

Initially it worked with Nginx as reverse proxy (keter version 0.1*), adding virtual server entries to its configuration and making Nginx reload it, but now Keter itself provides its own reverse proxy functionality, removing Nginx dependency and acting as the main web server.[20]

Old documentation (Nginx based).[21][22]

Integration with JavaScript generated from functional languages


See ref.[23][24][25]


  1. "Book - Initializing data in the foundation datatype". Retrieved 2014-05-26.
  2. a b c "book - Persistent". Retrieved 2012-10-23.
  3. "Yesod-persistent package". Retrieved 2012-10-23.
  4. "Yesod-persistent docs". Retrieved 2018-10-16.
  5. "Yesod-persistent entity syntax". Retrieved 2018-10-16.
  6. "Redundant migrations for fields' default values". Retrieved 2012-12-04.
  7. ""At most one" cardinality enforcement in persistent with type Checkmark". Retrieved 2018-10-16.
  8. "How can I create a foreign key constraint using Yesod/Persistent?". Retrieved 2018-10-16.
  9. "esqueleto package". Retrieved 2012-10-23.
  10. "Query example at". 2012-09-19. Retrieved 2012-10-23.
  11. "The yesod package". Retrieved 2019-06-26.
  12. "The email-validate package". Retrieved 2012-10-26.
  13. "The mime-mail package". Retrieved 2012-10-26.
  14. "The yesod-fb package". Retrieved 2012-10-26.
  15. Haskell Stack - How to install
  16. The yesod-bin pkg with the helper tool (with instructions for use with the stack tool)
  17. "book - Deploying your Webapp". Retrieved 2012-10-23.
  18. Readme.Md. "Yesod keter readme". GitHub. Retrieved 2012-10-23.
  19. "The keter package". Retrieved 2012-10-23.
  20. "Keter updates". 2012-10-25. Retrieved 2012-10-25.
  21. "Keter: Web App Deployment". 2012-05-11. Retrieved 2012-10-23.
  22. "Keter: It's Alive!". 2012-05-17. Retrieved 2012-10-23.
  23. "Javascript Options". Retrieved 2014-03-12.
  24. "Yesod, AngularJS and Fay". 2012-10-30. Retrieved 2014-03-12.
  25. "HaskellWiki - The JavaScript Problem". Retrieved 2014-04-12.