shakespeare-2.0.11.2: A toolkit for making compile-time interpolated templates

Safe Haskell | None
Language | Haskell98

Text.Hamlet.Runtime

Description

Module for parsing and rendering Hamlet templates at runtime, not compile time. This uses the same Hamlet parsing as compile-time Hamlet, but has some limitations, such as:

- No compile-time checking of validity
- Can't apply functions at runtime
- No URL rendering

    {-# LANGUAGE OverloadedStrings #-}
    import Text.Hamlet.Runtime
    import qualified Data.Map as Map
    import Text.Blaze.Html.Renderer.String (renderHtml)

    main :: IO ()
    main = do
        template <- parseHamletTemplate defaultHamletSettings $ unlines
            [ "<p>Hello, #{name}"
            , "$if hungry"
            , "  <p>Available food:"
            , "  <ul>"
            , "    $forall food <- foods"
            , "      <li>#{food}"
            ]
        let hamletDataMap = Map.fromList
                [ ("name", "Michael")
                , ("hungry", toHamletData True) -- always True
                , ("foods", toHamletData
                    [ "Apples"
                    , "Bananas"
                    , "Carrots"
                    ])
                ]
        html <- renderHamletTemplate template hamletDataMap
        putStrLn $ renderHtml html

Since: 2.0.6

Synopsis

- data HamletTemplate
- data HamletSettings
- defaultHamletSettings :: HamletSettings
- data HamletData
- class ToHamletData a where
- parseHamletTemplate :: MonadThrow m => HamletSettings -> String -> m HamletTemplate
- readHamletTemplateFile :: (MonadThrow m, MonadIO m) => HamletSettings -> FilePath -> m HamletTemplate
- renderHamletTemplate :: MonadThrow m => HamletTemplate -> Map Text HamletData -> m Html

# Documentation

data HamletTemplate #

A parsed Hamlet template. See `parseHamletTemplate` and `readHamletTemplateFile`.

Since: 2.0.6

data HamletSettings #

Settings for parsing of a hamlet document.

Instances

Lift HamletSettings # |
Methods lift :: HamletSettings -> Q Exp #

defaultHamletSettings :: HamletSettings #

Defaults settings: HTML5 doctype and HTML-style empty tags.

data HamletData #

A piece of data that can be embedded and passed to a Hamlet template (via `renderHamletTemplate`).

This supplies an `IsString` instance, so with `OverloadedStrings` it will support literal strings, which are converted to HTML via `toHtml`. For other datatypes, use `toHamletData`.

Since: 2.0.6

Instances

IsString HamletData # |
Methods fromString :: String -> HamletData #
ToHamletData HamletData # |
Methods toHamletData :: HamletData -> HamletData #

class ToHamletData a where #

Data which can be passed to a Hamlet template.

Since: 2.0.6

Minimal complete definition

toHamletData

Methods

toHamletData :: a -> HamletData #

Instances

ToHamletData Bool # |
Methods toHamletData :: Bool -> HamletData #
ToHamletData Text # |
Methods toHamletData :: Text -> HamletData #
ToHamletData Html # |
Methods toHamletData :: Html -> HamletData #
ToHamletData HamletData # |
Methods toHamletData :: HamletData -> HamletData #
(~) * a HamletData => ToHamletData [a] # |
Methods toHamletData :: [a] -> HamletData #
(~) * a HamletData => ToHamletData (Maybe a) # |
Methods toHamletData :: Maybe a -> HamletData #

parseHamletTemplate :: MonadThrow m => HamletSettings -> String -> m HamletTemplate #

Parse an in-memory Hamlet template. This operation may fail if the template is not parsable.

Since: 2.0.6

readHamletTemplateFile :: (MonadThrow m, MonadIO m) => HamletSettings -> FilePath -> m HamletTemplate #

Same as `parseHamletTemplate`, but reads from a file. The file is assumed to be UTF-8 encoded (same assumption as compile-time Hamlet).

Since: 2.0.6

renderHamletTemplate :: MonadThrow m => HamletTemplate -> Map Text HamletData -> m Html #

Render a runtime Hamlet template, together with a `Map` of variables to pass in, into an `Html` value. This can fail if the template references a variable that is not present in the `Map`.

Since: 2.0.6