yesod-examples 0.7.0.1 → 0.8.0
raw patch · 13 files changed
+49/−37 lines, 13 filesdep +persistent-templatedep +textdep ~yesod
Dependencies added: persistent-template, text
Dependency ranges changed: yesod
Files
- src/ajax.lhs +1/−1
- src/blog.lhs +6/−6
- src/chat.hs +5/−3
- src/file-echo.lhs +3/−2
- src/form.lhs +4/−2
- src/generalized-hamlet.lhs +1/−1
- src/i18n.lhs +8/−6
- src/pretty-yaml.lhs +1/−1
- src/session.lhs +1/−1
- src/widgets.lhs +5/−4
- synopsis/hamlet.lhs +5/−4
- synopsis/persistent.lhs +3/−2
- yesod-examples.cabal +6/−4
src/ajax.lhs view
@@ -2,7 +2,7 @@ <p>We're going to use jQuery for the Javascript, though anything would work just fine. Also, the AJAX responses will be served as JSON. Let's get started.</p> -> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses #-}+> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-} > import Yesod > import Yesod.Helpers.Static > import Data.Monoid (mempty)
src/blog.lhs view
@@ -2,7 +2,7 @@ <p>This file is literate Haskell, so we'll start off with our language pragmas and import statements. Basically every Yesod application will start off like this:</p> -> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses #-}+> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-} > import Yesod Next, we'll define the blog entry information. Usually, we would want to store the data in a database and allow users to modify them, but we'll simplify for the moment.@@ -29,7 +29,7 @@ Now we use the first "magical" Yesod set of functions: mkYesod and parseRoutes. If you want to see *exactly* what they do, look at their Haddock docs. For now, we'll try to keep this tutorial simple: -> mkYesod "Blog" [$parseRoutes|+> mkYesod "Blog" [parseRoutes| > / HomeR GET > /entry/#String EntryR GET > |]@@ -68,7 +68,7 @@ And now the template itself: > entryTemplate :: TemplateArgs -> Hamlet (Route Blog)-> entryTemplate args = [$hamlet|+> entryTemplate args = [hamlet| > !!! > > <html>@@ -97,8 +97,8 @@ > (entry:_) -> do > let nav = reverse $ map toNav entries > let tempArgs = TemplateArgs-> { templateTitle = string $ entryTitle entry-> , templateContent = string $ entryContent entry+> { templateTitle = toHtml $ entryTitle entry+> , templateContent = toHtml $ entryContent entry > , templateNavbar = nav > } > hamletToRepHtml $ entryTemplate tempArgs@@ -106,7 +106,7 @@ > toNav :: Entry -> Nav > toNav e = Nav > { navUrl = EntryR $ entrySlug e-> , navTitle = string $ entryTitle e+> , navTitle = toHtml $ entryTitle e > } All that's left now is the main function. Yesod is built on top of WAI, so you can use any WAI handler you wish. For the tutorials, we'll use the basicHandler that comes built-in with Yesod: it serves content via CGI if the appropriate environment variables are available, otherwise with simpleserver.
src/chat.hs view
@@ -1,6 +1,7 @@ {-# LANGUAGE TemplateHaskell, QuasiQuotes, TypeFamilies #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE OverloadedStrings #-} module Main where @@ -12,9 +13,10 @@ import Control.Concurrent.STM.TVar import Control.Arrow ((***))+import Data.Text (Text, unpack) -- speaker and content-data Message = Message String String+data Message = Message Text Text type Handler yesod = GHandler yesod yesod @@ -87,7 +89,7 @@ c <- lookupGetParam "client" case c of Nothing -> invalidArgs ["No client value in Check request"]- Just c' -> return $ read c'+ Just c' -> return $ read $ unpack c' cs <- liftIO . atomically $ readTVar clients chan <- case lookup client cs of Nothing -> invalidArgs ["Bad client value"]@@ -97,7 +99,7 @@ let Message s c = first jsonToRepJson $ zipJson ["sender", "content"] [s,c] -zipJson x y = jsonMap $ map (id *** jsonScalar) $ zip x y+zipJson x y = jsonMap $ map (unpack *** jsonScalar . unpack) $ zip x y getPostR :: Handler Chat RepJson getPostR = do
src/file-echo.lhs view
@@ -1,8 +1,9 @@-> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses #-}+> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-} > import Yesod > import Data.Monoid (mempty) > import qualified Data.ByteString.Char8 as S8+> import qualified Data.Text as T > data Echo = Echo @@ -24,6 +25,6 @@ > postHomepage = do > (_, files) <- runRequestBody > fi <- maybe notFound return $ lookup "file" files-> return [(S8.pack $ fileContentType fi, toContent $ fileContent fi)]+> return [(S8.pack $ T.unpack $ fileContentType fi, toContent $ fileContent fi)] > main = warpDebug 3000 Echo
src/form.lhs view
@@ -1,9 +1,11 @@ <p>Forms can be a tedious part of web development since they require synchronization of code in many different areas: the HTML form declaration, parsing of the form and reconstructing a datatype from the raw values. The Yesod form library simplifies things greatly. We'll start off with a basic application.</p> -> {-# LANGUAGE TypeFamilies, QuasiQuotes, OverloadedStrings, MultiParamTypeClasses #-}+> {-# LANGUAGE TypeFamilies, QuasiQuotes, OverloadedStrings, MultiParamTypeClasses, TemplateHaskell #-} > import Yesod > import Control.Applicative+> import Data.Text (Text)+ > data FormExample = FormExample > type Handler = GHandler FormExample FormExample > mkYesod "FormExample" [$parseRoutes|@@ -13,7 +15,7 @@ Next, we'll declare a Person datatype with a name and age. After that, we'll create a formlet. A formlet is a declarative approach to forms. It takes a Maybe value and constructs either a blank form, a form based on the original value, or a form based on the values submitted by the user. It also attempts to construct a datatype, failing on validation errors. -> data Person = Person { name :: String, age :: Int }+> data Person = Person { name :: Text, age :: Int } > deriving Show > personFormlet p = fieldsToTable $ Person > <$> stringField "Name" (fmap name p)
src/generalized-hamlet.lhs view
@@ -11,7 +11,7 @@ This example uses all three. You are of course free in your own code to make your own instances. -> {-# LANGUAGE QuasiQuotes, TypeFamilies, MultiParamTypeClasses #-}+> {-# LANGUAGE QuasiQuotes, TypeFamilies, MultiParamTypeClasses, OverloadedStrings, TemplateHaskell #-} > import Yesod > data NewHamlet = NewHamlet > mkYesod "NewHamlet" [$parseRoutes|/ RootR GET|]
src/i18n.lhs view
@@ -2,16 +2,18 @@ > {-# LANGUAGE TemplateHaskell #-} > {-# LANGUAGE TypeFamilies #-} > {-# LANGUAGE MultiParamTypeClasses #-}+> {-# LANGUAGE OverloadedStrings #-} > import Yesod > import Data.Monoid (mempty)+> import Data.Text (Text) > data I18N = I18N > type Handler = GHandler I18N I18N > mkYesod "I18N" [$parseRoutes| > / HomepageR GET-> /set/#String SetLangR GET+> /set/#Text SetLangR GET > |] > instance Yesod I18N where@@ -22,12 +24,12 @@ > ls <- languages > let hello = chooseHello ls > let choices =-> [ ("en", "English")+> [ ("en", "English") :: (Text, Text) > , ("es", "Spanish") > , ("he", "Hebrew") > ] > defaultLayout $ do-> setTitle $ string "I18N Homepage"+> setTitle "I18N Homepage" > addHamlet [$hamlet| > <h1>#{hello} > <p>In other languages:@@ -37,13 +39,13 @@ > <a href="@{SetLangR (fst choice)}">#{snd choice} > |] -> chooseHello :: [String] -> String+> chooseHello :: [Text] -> Text > chooseHello [] = "Hello"-> chooseHello ("he":_) = "שלום"+> chooseHello ("he":_) = "Shalom" > chooseHello ("es":_) = "Hola" > chooseHello (_:rest) = chooseHello rest -> getSetLangR :: String -> Handler ()+> getSetLangR :: Text -> Handler () > getSetLangR lang = do > setLanguage lang > redirect RedirectTemporary HomepageR
src/pretty-yaml.lhs view
@@ -1,6 +1,6 @@ <p>This example uses the <a href="http://hackage.haskell.org/package/data-object-yaml">data-object-yaml package</a> to display YAML files as cleaned-up HTML. If you've read through the other tutorials, this one should be easy to follow.</p> -> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses #-}+> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-} > import Yesod > import Data.Object
src/session.lhs view
@@ -1,4 +1,4 @@-> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses #-}+> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-} > import Yesod > import Control.Applicative ((<$>), (<*>)) >
src/widgets.lhs view
@@ -1,9 +1,10 @@-> {-# LANGUAGE TypeFamilies, QuasiQuotes, OverloadedStrings, MultiParamTypeClasses #-}+> {-# LANGUAGE TypeFamilies, QuasiQuotes, OverloadedStrings, MultiParamTypeClasses, TemplateHaskell #-} > import Yesod > import Yesod.Helpers.Static > import Yesod.Form.Jquery > import Yesod.Form.Nic > import Control.Applicative+> import Data.Text (unpack) > > data HW = HW { hwStatic :: Static } > type Handler = GHandler HW HW@@ -82,7 +83,7 @@ > getAutoCompleteR = do > term <- runFormGet' $ stringInput "term" > jsonToRepJson $ jsonList-> [ jsonScalar $ term ++ "foo"-> , jsonScalar $ term ++ "bar"-> , jsonScalar $ term ++ "baz"+> [ jsonScalar $ unpack term ++ "foo"+> , jsonScalar $ unpack term ++ "bar"+> , jsonScalar $ unpack term ++ "baz" > ]
synopsis/hamlet.lhs view
@@ -1,8 +1,9 @@ \begin{code}-{-# LANGUAGE QuasiQuotes #-}+{-# LANGUAGE QuasiQuotes, OverloadedStrings #-} import Text.Hamlet import qualified Data.ByteString.Lazy as L+import Data.Text (Text, cons) data Person = Person { name :: String@@ -11,11 +12,11 @@ , isMarried :: Bool , children :: [String] }-data PersonUrls = Homepage | PersonPage String+data PersonUrls = Homepage | PersonPage Text -renderUrls :: PersonUrls -> [(String, String)] -> String+renderUrls :: PersonUrls -> [(Text, Text)] -> Text renderUrls Homepage _ = "/"-renderUrls (PersonPage name) _ = '/' : name+renderUrls (PersonPage name) _ = '/' `cons` name footer :: Hamlet url footer = [$hamlet|\
synopsis/persistent.lhs view
@@ -1,13 +1,14 @@ This example uses the sqlite backend for Persistent, since it can run in-memory and has no external dependencies. -> {-# LANGUAGE TypeFamilies, GeneralizedNewtypeDeriving, QuasiQuotes #-}+> {-# LANGUAGE TypeFamilies, GeneralizedNewtypeDeriving, QuasiQuotes, TemplateHaskell, OverloadedStrings #-} > > import Database.Persist.Sqlite+> import Database.Persist.TH > import Control.Monad.IO.Class (liftIO) > > mkPersist [$persist|Person > name String Eq-> age Int update+> age Int Update > |] > > main :: IO ()
yesod-examples.cabal view
@@ -1,8 +1,8 @@ Name: yesod-examples-Version: 0.7.0.1+Version: 0.8.0 Synopsis: Example programs using the Yesod Web Framework. Description: These are the same examples and tutorials found on the documentation site.-Homepage: http://docs.yesodweb.com/+Homepage: http://www.yesodweb.com/ License: BSD3 License-file: LICENSE Author: Michael Snoyman@@ -18,7 +18,7 @@ Executable blog Main-is: src/blog.lhs Build-depends: base >= 4 && < 5,- yesod >= 0.7 && < 0.8+ yesod >= 0.8 && < 0.9 Executable ajax Main-is: src/ajax.lhs@@ -26,6 +26,7 @@ Executable file-echo Main-is: src/file-echo.lhs+ Build-depends: text Executable pretty-yaml Main-is: src/pretty-yaml.lhs@@ -52,7 +53,8 @@ Executable persistent-synopsis Main-is: synopsis/persistent.lhs Build-depends: transformers >= 0.2.1 && < 0.3,- persistent-sqlite+ persistent-sqlite,+ persistent-template Executable hamlet-synopsis Main-is: synopsis/hamlet.lhs