Interactive code snippets not yet available for SoH 2.0, see our Status of of School of Haskell 2.0 blog post

An Interactive Yesod Tutorial

The following code run a server which listen on / and /echo/*

If you go on / you are given a list of links pointing to url of the kind /echo/*. Click on them and you get a fading echo. Try it to see what I mean.

-- show
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses,
             TemplateHaskell, OverloadedStrings #-}
import Yesod
data MyApp = MyApp
instance Yesod MyApp -- We Declare MyApp to be a Yesod App

-- We declare the active URLs
mkYesod "MyApp" [parseRoutes|
  /             HomeR GET
  /echo/#String EchoR GET
|]

-- getHomeR doesn't take any parameter
-- return an HTML page
-- with two links to the same local URL
-- /echo/Le%20Big%20Mac
getHomeR = defaultLayout [whamlet|
  <ul>
   <li><a href=/echo/Yesod>to /echo/Yesod
   <li><a href=@{EchoR "Yesod"}>to @{EchoR "Yesod"}
|]

-- getEchoR take as parameter the content of the first
-- url variable declared in the route part
-- Then it write it many time fading :)
getEchoR theText = defaultLayout [whamlet|
    #{theText}
      <sup style="opacity:0.9">#{theText}
        <sup style="opacity:0.8">#{theText}
          <sup style="opacity:0.7">#{theText}
            <sup style="opacity:0.6">#{theText}
              <sup style="opacity:0.5">#{theText}
    <br>
    <a href=@{HomeR}>go Back
    |]

main = warpEnv MyApp

ex 1: Modify the links such that, when clicking on them you get the echo of "Hello"

ex 2: Modify the links such that, when clicking on them you get the echo of "Le Big Mac"

ex 3: Modify the code such that, the active route is no more /echo/* but /le/*

ex 4: Modify the code such that, it will add "le " before the repeating text.

ex 5: Modify the code such that, the active route is now /le/<some number>/*

ex 6: Modify the code such that, the number of echo is given by the number in the URL parameter.

Hint: You can call an Handler function using ^{function} in hamlet.

Hope you had fun!