Question in title. Do function component attributes have to be html safe? Is it not possible to pass a struct into my function component and use its various parameters which are html safe?
I’m having the hardest time verifying this in the docs, any help would be appreciated.
All the assigns that are used in html templates are escaped. If you are looking to have custom html in your assigns being rendered you can use raw:
<%= raw "<hello>" %>
Like, you can do:
def card(assigns) do
~H"""
<%= @user.name %>
"""
end
The following is to keep this small, %User{}
should be assigned:
<.card user={%User{name: "Bender"}}>
Is that what you’re asking? Otherwise, what @D4no0 said.
When you interpolate a value in HEEx, like <%= @name %>
, the HEEx engine will call Phoenix.HTML.Safe.to_iodata/1
, which is responsible for converting the value to HTML-escaped iodata.
This function is a protocol, so you could define an implementation for your own structs, but I don’t think it’s necessarily a good idea for “business entities” – a function component would be more idiomatic, as suggested above.
Thanks everyone. @sodapopcan that is indeed what I was asking. Turns out I was just missing the dot before the component name:
# what I had -- wrong
<my_component thing={@my_thing} />
All I needed was
<.my_component thing={@my_thing} />
Should’ve stretched my eyes instead of chasing ghosts for a couple of hours!