View Source ExMustache Hex.pm

ExMustache is fast mustache templating library for Elixir.

ExMustache supports everything except lambda from mustache spec. It is faster and uses lesser memory when compared to alternatives. ExMustache is a pure elixir implementation.

Installation

def deps do
  [
    {:ex_mustache, "~> x.x.x"}
  ]
end

Usage

# Parse once
mustache_template = "Hello {{name}}\n You have just won {{value}} dollars"
template = ExMustache.parse(mustache_template)


# Render multiple times
iodata = ExMustache.render(template, %{"name" => "Chris", "value" => 10000})
IO.puts(iodata)

iodata = ExMustache.render(template, %{"name" => "Bob", "value" => 20000})
IO.puts(iodata)

Note that render/2 returns iodata, which an efficient data structure for IO. Most of the IO operation accepts iodata so we can pass the rendered content without converting it to a binary. If you need rendered content as string, IO.iodata_to_binary/1 can be used. More about iodata

Performance

With my unscientific benchmarking, when compared to bbmustache (which is already a fast implementation), ExMustache is from 5 to 2 times faster and use 20 to 2 times lesser memory for redering. You can check the results here. These numbers vary a lot depending on the template, so take these number with grain of salt and please benchmark for your usecase if you are concerned with the performance. Check benchmark script sample case. Worst case scenario it should be at least as good as bbmustache.

Since ExMustache is optimized for rendering, parsing might use 4 to 2 times more memory compared to bbmustache.

Partial templates should be avoided to get better performance.