ucbi / uniform

Write less boilerplate and reuse more code in your portfolio of Elixir apps
https://hex.pm/packages/uniform
Apache License 2.0
33 stars 1 forks source link

Compile templates in blueprint #36

Closed paulstatezny closed 2 years ago

paulstatezny commented 2 years ago

Just like Phoenix.View, this PR compiles the EEx templates ahead of time during Elixir compilation.

Pros/Cons

The main purpose of making this change is:

  1. To allow depends_on?(app, :mix, :foo) instead of depends_on?.(app, :mix, :foo) (Note the . in the last example)
  2. To allow users to call any function in the Blueprint (or imported into it) in any template – calling fun_name(app) rather than having to call MyApp.Uniform.Blueprint.fun_name(app).

The tradeoff is that the implementation is a bit more complicated.

Implementation Details

It adds them as functions in Blueprint just like Phoenix.View places the templates in the View module.

Specifically, it creates functions whose name is the path of the template.

So if your templates directory is lib/my_app/uniform/templates and you have a template config/runtime.exs.eex, it will compile like this under the hood:

defmodule MyApp.Uniform.Blueprint do
  def :"config/runtime.exs"(app) do
    # function that returns rendered template from `lib/my_app/uniform/templates/config/runtime.exs.eex`
  end
end

(Of course, that pseudocode syntax won't compile because you can't create function names like that without macros.)