Personalizing messages using a text template
Sometimes we have a large list of usernames and relating data and we wish to individually send each person a message. This recipe will create a text template that will be filled out from the data.
Getting ready
Install the template
library using cabal:
$ cabal install template
How to do it…
Perform the following steps in a new file called Main.hs
:
Import the libraries as follows:
{-# LANGUAGE OverloadedStrings #-} import qualified Data.ByteString.Lazy as S import qualified Data.Text as T import qualified Data.Text.IO as TIO import qualified Data.Text.Lazy.Encoding as E import qualified Data.ByteString as BS import Data.Text.Lazy (toStrict) import Data.Text.Template
Define the data we are dealing with as follows:
myData = [ [ ("name", "Databender"), ("title", "Dr.") ], [ ("name", "Paragon"), ("title", "Master") ], [ ("name", "Marisa"), ("title", "Madam") ] ]
Define the template for the data as follows:
myTemplate = template "Hello...