Hey, I wrote a small macro to render .html.heex
file as a template in a phoenix functional component. The usage and macro definitions are below. The basic approach is to insert the compiled heex template at compile time. It’s essentially a copy-paste of Phoenix.LiveView.Helpers.sigil_H
. What would be the downsides of using this approach? Also, is there a plan to support rendering of .html.heex
for stateless components, in the future?
The usage looks something like this:
defmodule AppWeb.BrandComponents do
use AppWeb.Component.ComponentTemplateHelper
def logo(assigns) do
component_template("logo.html.heex")
end
end
Here is the macro definition
defmodule AppWeb.Component.ComponentTemplateHelper do
defmacro __using__(_info) do
quote do
import unquote(__MODULE__)
end
end
defmacro component_template(rel_path) do
# Not sure about the file, line, module options
options = [
engine: Phoenix.LiveView.HTMLEngine,
file: __CALLER__.file,
line: __CALLER__.line,
module: __CALLER__.module
]
file_path =
__CALLER__.file
|> Path.dirname()
|> Path.relative_to_cwd()
|> Path.join(rel_path)
Module.put_attribute(__CALLER__.module, :external_resource, file_path)
compiled =
file_path
|> File.read!()
|> EEx.compile_string(options)
quote do
unquote(compiled)
end
end
end