In our Elixir project, we couldn't save partials in the filesystem. To work around this, we wrapped bbmustache
like so:
defmodule OurApp.Mustache do
@partial_templates %{
foo: "Foo",
bar: "Bar"
}
def render(bin, data) do
{:bbmustache, tags, partial_keys, options, indents, context_stack} =
:bbmustache.parse_binary(bin)
:bbmustache.compile(
{
:bbmustache,
tags,
parse_remaining_partials(partial_keys, [], []),
options,
indents,
context_stack
},
data,
key_type: :atom,
raise_on_context_miss: true
)
end
defp parse_remaining_partials([] = _keys, partials, _parsed_keys), do: partials
defp parse_remaining_partials([key | rest_of_keys], partials, parsed_keys) do
if key in parsed_keys do
parse_remaining_partials(rest_of_keys, partials, parsed_keys)
else
bin = Map.fetch!(@partial_templates, String.to_existing_atom(key))
{:bbmustache, tags, nested_keys, _options, _indents, _context_stack} =
:bbmustache.parse_binary(bin)
parse_remaining_partials(rest_of_keys ++ nested_keys, [{key, tags} | partials], [
key | parsed_keys
])
end
end
end
I'd love to submit a PR but my Erlang skills are severely lacking.