inserts-0.1.2: Stupid simple bytestring templates.

CopyrightJoseph Abrahamson 2014
Safe HaskellNone



Completely trivial, interpolation-only Templates; for when you want an API that fits on a business card. Text.Template.Inserts implements a subset of Mustache syntax. It uses template strings with named holes deliminted by "mustaches":

import Data.HashMap.Strict as Map
import Data.ByteString     as S

context :: HashMap ByteString ByteString
context = Map.fromList [ ("country", "Morocco")
                       , ("favoriteFruit", "bananas")
>>> runTemplate (flip Map.lookup context) "I live in {{country}} and love {{favoriteFruit}}."
Right "I live in Morocco and love bananas"
>>> runTemplate (flip Map.lookup context) "My address is {{ address }}"
Left ["address"]

Text.Template.Inserts seeks to be as unsurprising and simple as possible sacrificing all kinds of niceities. Sometimes though, all you need is obvious, trivial string interpolation



data Template Source


Show Template 
IsString Template

Template literals can be embedded directly in Haskell files.

Monoid Template

O(1) appends

runTemplate :: (ByteString -> Maybe ByteString) -> Template -> Either [ByteString] ByteString Source

Outputs either the successfully interpolated template or the list of missing keys. For fast operation, try building the lookup function using unordered-containers HashMaps.

templateParser :: Parser Template Source

An attoparsec Parser for Templates. This is useful if you'd like to embed Templates into a more sophisticated, parseable type of your own.