Created
May 22, 2012 12:34
-
-
Save markhepburn/2768787 to your computer and use it in GitHub Desktop.
Basic yesod app from the first few chapters, just for including in a post.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, | |
TemplateHaskell, OverloadedStrings #-} | |
import Yesod | |
-- The app is parameterised on a custom datatype; this can also | |
-- contain other info of course: | |
data Links = Links | |
-- Routes, using template-haskell. (I find "Links" as a string to be | |
-- a bit icky, but otherwise it works quite nicely). 'R' as a suffix | |
-- is just a convention, but means "Resource". Note that HomeR, etc | |
-- become members of another data type created automatically. | |
mkYesod "Links" [parseRoutes| | |
/ HomeR GET | |
/page1 Page1R GET | |
/page2 Page2R GET | |
|] | |
-- Instantiate the Yesod interface for the app data type: the defaults | |
-- are fine here, but there's heaps of methods you can override to | |
-- specify things like the URL root, customise the default layout, | |
-- etc. | |
instance Yesod Links | |
-- Route handling. The mian thing here -- the main thing in yesod, | |
-- really -- is that because it's (template-)haskell again these are | |
-- checked at compile-time, so they're type-safe. Also, get*R is | |
-- generated by mkYesod above. | |
getHomeR, getPage1R, getPage2R :: Handler RepHtml | |
getHomeR = defaultLayout [whamlet|<a href=@{Page1R}>Go to page 1!|] | |
getPage1R = defaultLayout [whamlet|<a href=@{Page2R}>Go to page 2!|] | |
getPage2R = defaultLayout [whamlet|<a href=@{HomeR}>Go home!|] | |
main :: IO () | |
main = warpDebug 3000 Links |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment