HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleshakespeare-2.1.0.1Haskell2010

Text.Hamlet.Runtime

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
  • 3 types
  • 1 class
  • 4 values
classclass ToHamletData a where
#

Data which can be passed to a Hamlet template.

Methods

Instances6ToHamletData