Permalink
Browse files

Subsite with typeclass contexts and type variables

  • Loading branch information...
jprider63 committed Apr 4, 2017
1 parent 359616e commit 67b978e9ac9cc979e09c8bb46303c624d39630e0
Showing with 40 additions and 0 deletions.
  1. +1 −0 Cookbook.md
  2. +39 −0 cookbook/Subsite-with-typeclass-contexts-and-type-variables.md
@@ -74,6 +74,7 @@ For non-Yesod code, see also [Snippets](https://github.com/yesodweb/yesod-cookbo
* [Pure-Haskell static subdomains](https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Pure-Haskell-static-subdomains.md)
* [Slugs](https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Slugs.md)
* [Using type-safe URLs from inside Javascript](https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Using-type-safe-urls-from-inside-javascript.md)
* [Subsite with typeclass contexts and type variables](https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Subsite-with-typeclass-contexts-and-type-variables.md)

## Ajax

@@ -0,0 +1,39 @@
Subsite with typeclass contexts and type variables
==================================================

Create subsite data type and routes with typeclass contexts and type variables.

```
{-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies, TypeFamilyDependencies, StandaloneDeriving, ViewPatterns, FlexibleInstances, FlexibleContexts #-}
module MySubsite.Route where
import Database.Persist.Class
import Yesod.Core
class (PersistEntity (MyType master), PathPiece (Key (MyType master))) => MyClass master where
type MyType master = t | t -> master
data MySubsite master = MySubsite
mkYesodSubData "(MyClass master) => MySubsite master" [parseRoutes|
/#{Key (MyType master)} MyHandler GET
|]
```

Create subsite handlers and dispatcher.

```
{-# LANGUAGE TemplateHaskell, ViewPatterns, FlexibleInstances, MultiParamTypeClasses, OverloadedStrings, FlexibleContexts #-}
module MySubsite where
import Database.Persist.Class
import Yesod.Core
import MySubsite.Route
getMyHandler :: (MyClass master) => Key (MyType master) -> HandlerT (MySubsite master) (HandlerT master IO) Html
getMyHandler mtId = error "Implement me"
instance (MyClass master) => YesodSubDispatch (MySubsite master) (HandlerT master IO) where
yesodSubDispatch = $(mkYesodSubDispatch resourcesMySubsite)
```

0 comments on commit 67b978e

Please sign in to comment.