FabienHenon
CompileError when trying to generate a module and a function with Module.create
Hi,
I’m trying to generate a module with a function at runtime (from an Phoenix LiveView handle_params function), but I’m not able to make my code work.
Here is a sample code that fails:
page = "TestPage"
mod_name = Module.concat(MyProject.Pages, page)
Module.create(
mod_name,
quote do
def render(assigns) do
:ok
end
end,
line: 1,
file: "no_file.exs"
)
When I run this code (inside a handle_params function from a LiveView) I get this CompileError:
error: cannot invoke remote function Phoenix.Component.Declarative.__pattern__!/2 inside a match
└─ no_file.exs:1: MyProject.Pages.TestPage.render/1
What does it mean?
Am i missing something?
My code seems pretty simple, but still, it does not work.
I’m not a macro expert, maybe there is something I misunderstood.
When I try the same code with no argument for the render function, it works.
But as soon as I add an argument, it fails.
Thank you for your help
Marked As Solved
frankdugan3
Try this in your macro:
def my_render(var!(assigns)) do
Also Liked
D4no0
Yeah, so liveview render callbacks are kinda special, they are supposed to return a template, not an atom as in your case.
Without a specific example of how you are using it, it will be hard to determine what you are doing incorrectly. It does seem like you are missing a huge amount of prerequisites, like imports for views/liveviews/components, etc. in your module declaration.







