Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Allow getting partials from other sources #44

Closed
aptinio opened this issue Mar 15, 2020 · 3 comments · Fixed by #45
Closed

Feature request: Allow getting partials from other sources #44

aptinio opened this issue Mar 15, 2020 · 3 comments · Fixed by #45

Comments

@aptinio
Copy link

aptinio commented Mar 15, 2020

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

However, dialyzer is complaining about the opacity of :bbmustache.template() being broken because it is matched against a pattern.

It would be nice if :bbmustache.template() weren't opaque. Better yet, it would be nice if :bbmustache.parse_option() allows get_partial_fun which defaults to the current implementation of reading the partial from a file.

I'd love to submit a PR but my Erlang skills are severely lacking.

@soranoba
Copy link
Owner

I understand your motivation and difficult to use local file I/O on many systems.

I think that it should keep bbmustache::template() is opaque, because bbmustache does not want to make any changes to the data structure externally.
If it changes, backward compatibility will be difficult.

It is a good policy to add an option to parse_option.

@soranoba
Copy link
Owner

https://github.com/soranoba/bbmustache/tree/v1.9.0
released.

@aptinio
Copy link
Author

aptinio commented Mar 18, 2020

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants