File: OverloadedHello.hs

package info (click to toggle)
haskell-template 0.2.0.10-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 76 kB
  • sloc: haskell: 252; makefile: 2
file content (20 lines) | stat: -rw-r--r-- 579 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{-# LANGUAGE OverloadedStrings #-}

module Main where

import qualified Data.ByteString.Lazy as S
import qualified Data.Text as T
import qualified Data.Text.Lazy.Encoding as E

import Data.Text.Template

-- | Create 'Context' from association list.
context :: [(T.Text, T.Text)] -> Context
context assocs x = maybe err id . lookup x $ assocs
  where err = error $ "Could not find key: " ++ T.unpack x

main :: IO ()
main = S.putStr $ E.encodeUtf8 $ substitute helloTemplate helloContext
  where
    helloTemplate = "Hello, $name!\n"
    helloContext  = context [("name", "Joe")]