Skip to content

Instantly share code, notes, and snippets.

@jmn
Created April 12, 2018 14:46
Show Gist options
  • Save jmn/c499b6ae396ca37e6ad8d8390a3eb21e to your computer and use it in GitHub Desktop.
Save jmn/c499b6ae396ca37e6ad8d8390a3eb21e to your computer and use it in GitHub Desktop.
Yesod.Lucid.hs
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- | Lucid support for Yesod.
--
-- Example Handler for a route, using Lucid to generate html,
-- including a rendered url:
--
-- > import Yesod.Lucid
-- > import Lucid
-- >
-- > getExampleR :: Handler LucidHtml
-- > getExampleR = lucid $ \url ->
-- > p_ $ a_ [href_ (url ExampleR)] "self link"
module YesodLucid where
import Control.Monad.Identity
import Data.Text (Text)
import Lucid
import Yesod.Core (ToTypedContent, MonadHandler, ToContent, Route,
HandlerSite, HasContentType(..))
import qualified Yesod.Core as Y
-- | Handler LucidHtml can be used for yesod handlers that use lucid to
-- generate html.
type LucidHtml = Html ()
-- | A lucid generator.
type LucidGen a = (Route a -> Text) -> LucidHtml
-- | Output some lucid, passes a URL renderer to the continuation.
lucid :: MonadHandler m => LucidGen (HandlerSite m) -> m LucidHtml
lucid cont = fmap cont Y.getUrlRender
instance ToTypedContent (Html ()) where
toTypedContent m = Y.TypedContent (getContentType (Just m)) (Y.toContent m)
instance ToContent (Html ()) where
toContent html = Y.ContentBuilder (runIdentity (execHtmlT html)) Nothing
instance HasContentType (Html ()) where
getContentType _ = "text/html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment