happstack-hamlet 0.1 → 0.2.1
raw patch · 2 files changed
+80/−2 lines, 2 filesdep ~hamlet
Dependency ranges changed: hamlet
Files
- demo.hs +77/−0
- happstack-hamlet.cabal +3/−2
+ demo.hs view
@@ -0,0 +1,77 @@+{-# LANGUAGE QuasiQuotes, ScopedTypeVariables, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, UndecidableInstances #-}+module Main where++import Control.Monad.Trans(MonadIO(liftIO))+import Data.Text (pack)+import Happstack.Server+import Happstack.Server.Hamlet+import Text.Hamlet+import Text.Hamlet.Monad (hamletToText, liftHamlet)++-- | type used to populate the template+data Person = Person+ { name :: IO HtmlContent -- maybe it requires a database lookup+ , age :: HtmlContent+ , page :: PersonUrls+ , isMarried :: Bool+ , children :: [HtmlContent]+ }++-- | type which represents paths to different pages on the site+data PersonUrls = Homepage | PersonPage String++-- | function to turn the url type into a string+renderUrls :: PersonUrls -> String+renderUrls Homepage = "/"+renderUrls (PersonPage name) = '/' : name++-- | hamlet template which generates page footer+footer :: Monad m => Hamlet url m ()+footer = [$hamlet|+#footer Thank you, come again+|]+++-- | hamlet template which generates a page+template :: Person -> Hamlet PersonUrls IO ()+template person = [$hamlet|+!!!+%html+ %head+ %title Hamlet Demo+ %body+ %h1 Information on $*name.person$+ %p $*name.person$ is $age.person$ years old.+ %h2+ $if isMarried.person+ <b>Married</b>+ $else+ Not <b>married</b>+ %ul+ $forall children.person child+ %li $child$+ %p+ %a!href=@page.person@ See the page.+ ^footer^+|]+++-- | some dummy content to use with the template+person :: Person+person = Person+ { name = return $ Unencoded $ pack "Michael"+ , age = Unencoded $ pack "twenty five & a half"+ , page = PersonPage "michael"+ , isMarried = True+ , children = [ Unencoded $ pack "Adam"+ , Unencoded $ pack "Ben"+ , Unencoded $ pack "Chris"+ ]+ }++-- | example of using 'hamletToResponse' to turn the 'Hamlet' template+-- into a 'ServerPartT IO Response'.+--+-- NOTE: we ignore the requested URL in this example for simplicity+main :: IO ()+main = simpleHTTP nullConf $ liftIO $ hamletToResponse renderUrls $ template person
happstack-hamlet.cabal view
@@ -1,5 +1,5 @@ Name: happstack-hamlet-Version: 0.1+Version: 0.2.1 Synopsis: Support for Hamlet HTML templates in Happstack Description: Happstack is a web application development framework. @@ -15,6 +15,7 @@ Category: Web Build-type: Simple Cabal-version: >=1.6+Extra-source-files: demo.hs source-repository head type: darcs@@ -27,7 +28,7 @@ bytestring, containers, happstack-server >= 0.4.3 && <0.6 ,- hamlet,+ hamlet >= 0.2 && <3, text -- Modules not exported by this package.