BSD-3-Clause licensed and maintained by Mark Karpov
This version can be pinned in stack with:stache-2.0.1@sha256:556e73d15229fa2df06d147172ad5855f23b851d93d6fbf01fd40e02a33caa9d,5210


License BSD3 Hackage Stackage Nightly Stackage LTS Build Status

This is a Haskell implementation of Mustache templates. The implementation conforms to the version 1.1.3 of the official Mustache specification. It is extremely simple and straightforward to use with 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 the Megaparsec parsing library to parse the templates which results in superior quality of error messages.

For rendering you only need to create Aeson’s Value where you put the data to interpolate. 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 whole process is very simple for the end user.

Template Haskell helpers for compilation of templates at compile time are available in the Text.Mustache.Compile.TH module. The helpers currently work only with GHC 8 and later.

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.

Quick start

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:


Issues, bugs, and questions may be reported in the GitHub issue tracker for this project.

Pull requests are also welcome and will be reviewed quickly.


Copyright © 2016–2018 Stack Builders

Distributed under BSD 3 clause license.


Stache 2.0.1

  • Now interpolation with escaping also escapes single quotes as &#39.

Stache 2.0.0

  • Uses Megaparsec 7. In particular, the parser now returns ParseErrorBundle on failure and the bundle is also inside MustacheException.

Stache 1.2.1

  • Fixed a bug in compileMustacheDir' from Text.Mustache.Compile.TH: it did not use the supplied predicate and selected only .mustache files like compileMustacheDir.

Stache 1.2.0

  • Added compileMustacheDir' and getMusthacheFilesInDir' that allow for a custom template predicate. Also exposed isMustacheFile as the default predicate used by functions like compileMustacheDir.

  • Abandon attempts to support GHC 7.8.

Stache 1.1.2

  • Fixed compilation of the test suite with Cabal 2.0/GHC 8.2.1.

Stache 1.1.1

  • Exported mustacheRenderW, MustacheWarning, and displayMustacheWarning from Text.Mustache.

Stache 1.1.0

  • Added mustacheRenderW that allows to render a Template collecting warnings in the process. Also added the MusthacheWarning type representing the warnings themselves and displayMustacheWarning to pretty-print the warnings.

Stache 1.0.0

  • Improved metadata and documentation.

  • Breaking change: the renderMustache function does not throw exceptions when referenced key is not provided as per the spec. This is the behaviour we had before 0.2.0, and it played better with the rest of Mustache. Correspondingly, MustacheRenderException was removed.

  • Stache now uses Megaparsec 6 for parsing.

  • MustacheException now includes original input as Text.

  • compileMustacheText and parseMustache now accept strict Text instead of lazy Text.

  • Minor improvement in rendering speed.

  • Stop depending on exceptions and drop MonadThrow constraints.

Stache 0.2.2

  • Add the getMustacheFilesInDir function.

  • Make TH helpers compileMustacheDir and compileMustacheFile detect changes in the templates and force recompilation.

Stache 0.2.1

  • Made TH parse errors nicer.

Stache 0.2.0

  • Breaking change: the renderMustache function will throw an exception when referenced key was not provided. This is a better behavior than silent interpolation of an empty string, because missing values are almost always a mistake and it’s easy to provide empty strings explicitly anyway.

  • Allowed directory-

Stache 0.1.8

  • Rename specs directory to specification as the previous name somehow caused conflicts when deploying an application on Heroku with Stache as a dependency.

Stache 0.1.7

  • Added mustache quasi-quoter.

Stache 0.1.6

  • Fixed a bug in the lookup algorithm for dot-separated keys.

Stache 0.1.5

  • When section’s key is a number or non-empty string, it’s accessible as . in the section body.

  • Allow Aeson 1.0.

Stache 0.1.4

  • Numbers are now treated as “true” values.

  • Added Semigroup instance for Key.

  • Now change of delimiters affects special unescaped variable syntax as well, for example: {{=<< >>=}}<<{var}>> will be parsed as unescaped variable var.

  • compileMustacheFile now shows full path to malformed template when the template cannot be parsed.

  • Defined displayException method of Exception type class for MustacheException using parseErrorPretty from Megaparsec.

Stache 0.1.3

  • Cosmetic improvements.

  • Minor improvement in performance of parser.

Stache 0.1.2

  • Fixed compilation of benchmarks with Megaparsec 5.0.1 and later.

Stache 0.1.1

  • Added benchmarks.

Stache 0.1.0

  • Initial release.