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

Safe Haskell | None
Language | Haskell2010

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
  - toHamletData :: a -> HamletData
- 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

Instances details

Lift HamletSettings # |
Instance details Defined in Text.Hamlet.Parse Methods lift :: Quote m => HamletSettings -> m Exp # liftTyped :: forall (m :: Type -> Type). Quote m => HamletSettings -> Code m HamletSettings #

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

Instances details

IsString HamletData # |
Instance details Defined in Text.Hamlet.Runtime Methods fromString :: String -> HamletData #
ToHamletData HamletData # |
Instance details Defined in Text.Hamlet.Runtime Methods toHamletData :: HamletData -> HamletData #

class ToHamletData a where #

Data which can be passed to a Hamlet template.

Since: 2.0.6

Methods

toHamletData :: a -> HamletData #

#### Instances

Instances details

ToHamletData Html # |
Instance details Defined in Text.Hamlet.Runtime Methods toHamletData :: Html -> HamletData #
ToHamletData HamletData # |
Instance details Defined in Text.Hamlet.Runtime Methods toHamletData :: HamletData -> HamletData #
ToHamletData Text # |
Instance details Defined in Text.Hamlet.Runtime Methods toHamletData :: Text -> HamletData #
ToHamletData Bool # |
Instance details Defined in Text.Hamlet.Runtime Methods toHamletData :: Bool -> HamletData #
a ~ HamletData => ToHamletData (Maybe a) # |
Instance details Defined in Text.Hamlet.Runtime Methods toHamletData :: Maybe a -> HamletData #
a ~ HamletData => ToHamletData [a] # |
Instance details Defined in Text.Hamlet.Runtime Methods toHamletData :: [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