Hi y’all. I’m trying to understand how Phoenix works but I’m seriously struggling with this. I’m trying to understand why this is valid code?
defmodule WorkoutGeneratorWeb.WorkoutHTML do
use WorkoutGeneratorWeb, :html
Renders a workout form.
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true
workout_form/1 (?) has a missing
end statement. This module was made from the typical generators (with no schema option).
answered by @sodapopcan. From the docs…
Embedded templates also support declarative assigns via bodyless function definitions, for example…
The answer is here.
In short, the body is defined from the heex template via macro magic.
A hint is that it’s not just missing and
end but also a
do. Maybe it’s too much info for now but a more “normal” example of this syntax in the wild is when defining multiple function heads that have a default pameter(s):
def foo(a, b \\ )
def foo(:bar, b) do
["bar" | b]
def foo(:baz, b) do
["baz" | b]
It is the function signature…
As mentionned by @sodapopcan, there is no block (do…end)
Thanks, I appreciate it. I guess it’s all about reading the docs more.
Hey, it’s cool. It can be overwhelming at first, and I agree it’s odd syntax to come across when first learning the language. That’s all part of what this forum is for!