Links und Funktionen
Sprachumschaltung

Navigationspfad


Inhaltsbereich

Yesod Message

Code Beispiel für die Standardverwendung von Messages

Haskell source code icon YesodMessage.hs — Haskell source code, 1 KB (1432 bytes)

Dateiinhalt

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}

module Main where

import Yesod
import Data.Text (pack,unpack)


data App = App
instance Yesod App


mkYesod "App" [parseRoutes|
/  HomeR GET
/A AR    GET
/B BR    GET
/C CR    GET
|]

sitemap = [HomeR,AR,BR,CR]
links = [whamlet|
          <div>
          <ul>
            $forall l <- sitemap
              <li><a href=@{l}>Link to @{l}
        |]

getHomeR :: Handler Html
getHomeR = do
  page <- defaultLayout $ do
    [whamlet|
      You are at Home!
    |]
    links
  setMessage "MSG: Previous: Home"
  return page

getAR :: Handler Html
getAR = do
  page <- defaultLayout $ do
    [whamlet|
      You are at A
    |]
    links
  setMessage "MSG: Previous: A"
  return page


getBR :: Handler Html
getBR = do
  -- Message mal woanders anzeigen:
  maybemsg <- getMessage
  -- Jetzt ist die Message gelöscht,
  -- weshalb defaultLayout diese nicht mehr anzeigt,
  -- und wir die Message selbst anzeigen müssen.
  page <- defaultLayout $ do
    [whamlet|
      <p>You are at B
      $maybe msg <- maybemsg
        <p>Message was: #{msg}
    |]
    links
  setMessage "MSG: Previous: B"
  return page

getCR :: Handler Html
getCR = do
  setMessage "MSG: Previous: C"
  defaultLayout $ do
    [whamlet|
      You are at C. NOTE: No Message is set here afterwards!
    |]
    links

main :: IO ()
main = warp 3000 App

Artikelaktionen


Funktionsleiste