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

Modulestache-2.3.4Haskell2010

Text.Mustache

This is a Haskell implementation of Mustache templates. The implementation conforms to the version 1.1.3 of the official Mustache specification https://github.com/mustache/spec. It has a minimal but complete API—three functions to compile templates (from directory, from file, and from lazy text) and one to render them.

The implementation uses Megaparsec parsing library to parse the templates which results in high-quality error messages.

For rendering you only need to create Aeson's Data.Aeson.Value that is used for interpolation of template variables. Since the library re-uses Aeson's instances and most data types in the Haskell ecosystem are instances of classes like Data.Aeson.ToJSON, the process is simple for the end user.

Template Haskell helpers for compilation of templates at compile time are available in the Text.Mustache.Compile.TH module.

One feature that is not currently supported is lambdas. The feature is marked as optional in the spec and can be emulated via processing of parsed template representation. The decision to drop lambdas is intentional, for the sake of simplicity and better integration with Aeson.

Here is an example of basic usage:

{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Data.Aeson
import Data.Text
import Text.Megaparsec
import Text.Mustache
import qualified Data.Text.Lazy.IO as TIO

main :: IO ()
main = do
  let res = compileMustacheText "foo"
        "Hi, {{name}}! You have:\n{{#things}}\n  * {{.}}\n{{/things}}\n"
  case res of
    Left bundle -> putStrLn (errorBundlePretty bundle)
    Right template -> TIO.putStr $ renderMustache template $ object
      [ "name"   .= ("John" :: Text)
      , "things" .= ["pen" :: Text, "candle", "egg"]
      ]

If I run the program, it prints the following:

Hi, John! You have:
  * pen
  * candle
  * egg

For more information about Mustache templates the following links may be helpful:

  • 6 types
  • 7 values
  • Packagestache-2.3.4
  • Exports13
  • LanguageHaskell2010
  • LicenceMIT
  • SourceMustache.hs

Types

7 declarations
datadata Template
#

Mustache template as the name of the “top-level” template and a collection of all available templates (partials).

Template is a Semigroup. This means that you can combine Templates (and their caches) using the (<>) operator, the resulting Template will have the same currently selected template as the left one. Union of caches is also left-biased.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Semigroup, …
datadata Node
#

A structural element of a template.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Lift, …
newtypenewtype Key
#

Identifier for values to interpolate.

The representation is the following:

  • []—empty list means implicit iterators;

  • [text]—single key is a normal identifier;

  • [text1, text2]—multiple keys represent dotted names.

Constructors

Instances10Eq, Data, Ord, Show, Generic, Semigroup, …
newtypenewtype PName
#

Identifier for partials. Note that with the OverloadedStrings extension you can use just string literals to create values of this type.

Constructors

Instances9Eq, Data, Ord, Show, IsString, Generic, …
newtypenewtype MustacheException
#

Exception that is thrown when parsing of a template fails or referenced values are not provided.

Constructors

Instances5Eq, Show, Generic, Exception, Rep
datadata MustacheWarning
#

Warning that may be generated during rendering of a Template.

Constructors

Instances4Eq, Show, Generic, Rep

Compiling

4 declarations
valuecompileMustacheDir
  1. :: MonadIO m
  2. => PName

    Which template to select after compiling

  3. -> FilePath

    Directory with templates

  4. -> m Template

    The resulting template

#

Compile all templates in the specified directory and select one. Template files should have the extension mustache, (e.g. foo.mustache) to be recognized. This function does not scan the directory recursively.

Note that each template/partial will get an identifier which consists of the name of corresponding template file with extension .mustache dropped. This is important for e.g. selecting active template after loading (the first argument).

The action can throw MustacheParserException and the same exceptions as getDirectoryContents, and readFile.

compileMustacheDir = complieMustacheDir' isMustacheFile

Rendering

2 declarations