{"id":32161068,"url":"https://github.com/ivanbakel/ytl","last_synced_at":"2025-10-21T13:50:15.353Z","repository":{"id":56883347,"uuid":"324248285","full_name":"ivanbakel/ytl","owner":"ivanbakel","description":"Yesod Transformer Library - MTL for site stacks","archived":false,"fork":false,"pushed_at":"2020-12-29T12:59:00.000Z","size":16,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-21T13:50:05.384Z","etag":null,"topics":["mtl","site-transformers","yesod"],"latest_commit_sha":null,"homepage":"","language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ivanbakel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-24T22:49:08.000Z","updated_at":"2023-02-16T12:05:53.000Z","dependencies_parsed_at":"2022-08-20T22:31:10.067Z","dependency_job_id":null,"html_url":"https://github.com/ivanbakel/ytl","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ivanbakel/ytl","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanbakel%2Fytl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanbakel%2Fytl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanbakel%2Fytl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanbakel%2Fytl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ivanbakel","download_url":"https://codeload.github.com/ivanbakel/ytl/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanbakel%2Fytl/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280272338,"owners_count":26302260,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-21T02:00:06.614Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mtl","site-transformers","yesod"],"created_at":"2025-10-21T13:50:10.466Z","updated_at":"2025-10-21T13:50:15.338Z","avatar_url":"https://github.com/ivanbakel.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ytl - Yesod Transformer Library\n\n[Yesod](https://github.com/yesodweb/yesod) is a powerful, type-safe webserver library. At its core, Yesod lets you describe *foundation sites* which carry the configuration of your webserver, and *handlers* and *widgets* which use that foundation to handle requests, generate HTML and CSS, and even access a database.\n\nHaskellers who are familiar with [mtl](https://github.com/haskell/mtl) are used to transforming monad stacks, adding or computing monadic features at whim, to introduce wide-ranging effects to a program with a few small, type-safe changes. Unfortunately, Yesod does not support monad transformers - its basic `HandlerFor` and `WidgetFor` monads are woven into the server code, and won't allow for other monads to stack on top.\n\nHowever, a `mtl`-style approach can be done for Yesod - the secret is, rather than transforming Yesod's `HandlerFor` and `WidgetFor` monads, to *transform the foundation site itself*. The result is `ytl` - the Yesod transfomer library, which exports a similar API to `mtl`, but in terms of site transformers.\n\nA site transformer wraps a Yesod foundation site in additional information. Yesod's code is fine with custom foundation types, so the wrapped site can be used in Yesod code as normal - giving additional effects to existing Yesod code for \"free\".\n\n## An example\n\nThe `ReaderSite` transformer adds additional data to a site, similar to how `ReaderT` adds data to a monad stack. The data accessible by `ReaderSite` can be accessed through a `MonadReader`-style API in Yesod's `HandlerFor` and `WidgetFor` monads - like this:\n\n```haskell\nuseValue\n  :: (SiteReader r site)\n  =\u003e HandlerFor site a\n  -\u003e (r -\u003e m site b)\n  -\u003e HandlerFor site (a, b)\nuseValue mArg f = do\n  a \u003c- mArg\n  r \u003c- ask\n  b \u003c- f r\n  pure (a, b)\n```\n\n## Writing your own transformers\n\nWriting your own `ytl` site transformers is relatively simple. Define your site wrapper, and some class for its API - in the above example, `ReaderSite` is the wrapper, and `SiteReader` is the API class.\n\nImplement the class for the wrapper (duh!). Then, to interact nicely with the other transformers, you'll want to define a lifting instance, which lifts the API implementation through any transformer layers. For `ReaderSite`, this looks like\n```haskell\ninstance {-# OVERLAPPABLE #-}\n  (SiteTrans t, SiteReader r site) =\u003e SiteReader r (t site) where\n  ...\n```\n\n### Implement `RenderRoute`, and maybe `Yesod`\n\nYou'll also need to implement `RenderRoute` for your wrapped site type - make sure to make the route type coercible to the route type of the base site, because `ytl` relies on them being representation-identical (see `SiteCompatible`)! This is normally fine, because you don't want to modify the routes anyways.\n\nIf your site transformer changes some `Yesod` behaviour - like logging, middleware, etc. - you need to also implement `Yesod` for your wrapped site type. `ytl` comes with some Template Haskell helpers for writing `Yesod` implementations that pass through to the base site most of the time; so if you just want to override how logging works, you can ignore the other `Yesod` methods and they'll be implemented for you.\n\n### Why isn't my translation of this mtl type working?\n\nRemember that, in Yesod's handlers and widgets, the site is in *reader* position - it gets passed into Yesod functions. That means that the site type is basically contravariant (as evidenced by `withSiteT`) - so `HandlerFor` and `WidgetFor` are an awful lot like profunctors. That means that you can't translate `mtl`-style code directly - you have to consider the difference in data flow.\n\nFor example, `ReaderSite` is not an exact copy of `ReaderT`: `ReaderT` takes the read value as an argument:\n```\ndata ReaderT m a = ReaderT (r -\u003e m a)\n```\nBut for Yesod, the site is already in reader position - taking `r` as an argument wouldn't have the right semantics. Instead, `ReaderSite` includes `r` *alongside* the site:\n```\ndata ReaderSite r site = ReaderSite r site\n```\nThat looks very different. However, they end up being very similar in use:\n```haskell\nrunReaderT :: ReaderT r m a -\u003e r -\u003e m a\n\nrunReaderSite :: HandlerFor (ReaderSite r site) a -\u003e r -\u003e HandlerFor site a\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivanbakel%2Fytl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivanbakel%2Fytl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivanbakel%2Fytl/lists"}