Closed azizk closed 3 weeks ago
Related: https://github.com/elixir-lang/elixir/pull/10566.
I think you may be able to write something like
<% hruler = fn assigns ->
~H|<hr style={assigns[:color] && "background-color: #{@color}"} />|
end %>
instead.
I'll say though that what you are doing there is very particular and could have some inadvertent effects on proper change tracking. Is there a reason not to define a separate function component for your hruler
?
Yes, that's a possible workaround, but it breaks as soon as you have to interpolate something with EEx tags (e.g. <%= @text %>
), because the parser doesn't understand that there is a nested HEEx template literal.
It seems peculiar and I don't know if it affects change tracking, but nevertheless it would be cool if it worked so we could define ad-hoc functions like that for repetetive stuff and not be required to touch the HTML
or Live
module.
Please do a bug report on Elixir. However, I must say that this should be discouraged, it will definitely have negative consequences when it comes to change tracking.
@josevalim I looked into it a little bit and the formatter changing it to <%=
is caused by this:
where we always force a <%=
for EEx blocks ignoring the opt. The warning is from the linked Elixir PR, but I'm not sure what Elixir should do differently?
I see, so it has to be an open issue in both places.
The issue is that both Elixir and LiveView are assuming that, if there is a block, we want to output the result. And this example shows that to be false. Perhaps we can do a more robust check in Elixir (for example, only emit the warning if the block is at the root of the AST node). The fix on LiveView should be simpler, we should instead preserve the block type.
I have a local branch to change it in LiveView, will try to push it up later today 👍
@SteffenDE I went ahead and pushed the fix over here https://github.com/phoenixframework/phoenix_live_view/pull/3299
Let me know if you thought something else and feel free to close in case you have another solution 🙌🏼. I haven't digg into the Elixir warning issue though, let me know if you are on it. I'm happy to help if you need anything
I'll leave this open to wait for the Elixir changes as well.
Environment
Actual behavior
The following HEEx code compiles with a warning but works as expected. However, when the code is formatted, the opening
<%
is changed to<%=
, which breaks the functionality.Expected behavior
It should compile without warnings, and formatting the code should not change
<%
to<%=
, preserving the functionality.