microstache: Mustache templates for Haskell

[ bsd3, library, text ] [ Propose Tags ] [ Report a vulnerability ]

Mustache templates for Haskell.

Based on stache library, which uses megaparsec. This library uses parsec, thus the name: microstache.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 1, 1.0.1, 1.0.1.1, 1.0.1.2, 1.0.2, 1.0.2.1, 1.0.2.2, 1.0.2.3, 1.0.3
Change log CHANGELOG.md
Dependencies aeson (>=2.2.2.0 && <2.3), base (>=4.12.0.0 && <4.21), containers (>=0.6.0.1 && <0.8), deepseq (>=1.4.4.0 && <1.6), directory (>=1.3.3.0 && <1.4), filepath (>=1.4.2.1 && <1.6), parsec (>=3.1.13.0 && <3.2), text (>=1.2.3.0 && <1.3 || >=2.0 && <2.2), transformers (>=0.5.6.2 && <0.7), unordered-containers (>=0.2.20 && <0.3), vector (>=0.13.1.0 && <0.14) [details]
Tested with ghc ==8.6.5 || ==8.8.4 || ==8.10.7 || ==9.0.2 || ==9.2.8 || ==9.4.8 || ==9.6.5 || ==9.8.2 || ==9.10.1
License BSD-3-Clause
Author Mark Karpov <markkarpov@openmailbox.org>, Oleg Grenrus <oleg.grenrus@iki.fi>
Maintainer Oleg Grenrus<oleg.grenrus@iki.fi>
Category Text
Home page https://github.com/haskellari/microstache
Bug tracker https://github.com/haskellari/microstache/issues
Source repo head: git clone https://github.com/haskellari/microstache.git
Uploaded by phadej at 2024-05-17T00:36:09Z
Distributions Arch:1.0.2.3, Debian:1.0.1.1, Fedora:1.0.2.3, LTSHaskell:1.0.2.3, NixOS:1.0.2.3, Stackage:1.0.3, openSUSE:1.0.3
Reverse Dependencies 3 direct, 3644 indirect [details]
Downloads 28748 total (233 in the last 30 days)
Rating 2.5 (votes: 3) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-05-17 [all 1 reports]

Readme for microstache-1.0.3

[back to package description]

microstache

Based on stache library, which uses megaparsec. This library uses parsec, thus the name: microstache.

This is a Haskell implementation of Mustache templates. The implementation conforms to the version 1.1.3 of official [Mustache specification] (https://github.com/mustache/spec). 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.

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 Haskell ecosystem are instances of classes like Data.Aeson.ToJSON, the whole process is very simple for end user.

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.

Differences from stache

  • Instead of megaparsec, parsec is used. Error message quality is most likely degraded.
  • There are no TemplateHaskell used; yet there are no helpers provided.
  • Support for GHC-7.4.2 – GHC-8.2.1

Quick start

Here is an example of basic usage:

{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Data.Aeson
import Data.Text
import Text.Microstache
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 err -> putStrLn (show err)
    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:

License

Copyright © 2016–2017 Stack Builders, 2017 Oleg Grenrus

Distributed under BSD 3 clause license.