pedromvieira
How to replace "direct/simple" sigil_E to new sigil_H to avoid warnings?
Updating to Elixir 1.13 and got several warnings about sigil_E deprecation:
warning: Phoenix.HTML.sigil_E/2 is deprecated. use the ~H sigil instead
Assuming that not everything is (and will be) LiveView yet, how to correct the simple / direct sigil_E to new implementations (and to avoid warnings)?
...
import Phoenix.HTML, only: [sigil_E: 2, safe_to_string: 1]
...
def my_fun(arg1) do
safe_html =
~E"""
<div class="<%= arg1.class %>">
<%= arg1.message %>
</div>
"""
_html =
safe_html
|> safe_to_string()
end
This results in:
SAFE_HTML
{:safe, ["<div class=\"myclass\">ALOHA</div>"]}
HTML
"<div class=\"myclass\">ALOHA</div>"
Marked As Solved
sodapopcan
Oh sorry, I misread.
I did some playing around (first time I made use of Mix.install/1 which was super cool!) and it looks like what you’re looking for is Phoenix.HTML.Safe.to_iodata/1 (see here). And for identical results you’re going to want to call List.to_string/1 on the result.
YouComponent.test_h()
|> Phoenix.HTML.Safe.to_iodata()
|> List.to_string()
EEx and heex work very differently as heex does change tracking.
I am curious as to why you need this, though. Other than having to change how attribute values are passed, changing to heex should just be a matter of just switching up the sigil.
Also Liked
schnittchen
For me it was not just replacing ~E with ~H and changing the HTML attribute syntax. I got undefined function assigns/0 each time and had to add assigns = %{} each time even though the ~H snippet did not use any assign. See also "undefined function assigns/0" while trying to use Phoenix.LiveViewTest.rendered_to_string/1 · Issue #1798 · phoenixframework/phoenix_live_view · GitHub
markmark206
In case this is useful for anyone, here is a working example of composing safe HTML text using an ~H block:
defmodule EmailBody do
import Phoenix.Component, only: [sigil_H: 2]
def compose(question_of_the_day) do
assigns = %{
question: question_of_the_day
}
~H"""
<div><%= @question %>?</div>
"""
|> Phoenix.HTML.Safe.to_iodata()
|> List.to_string()
end
end
$ iex -S mix
...
iex(20)> "Why did the chicken cross the road (and here is a rogue div: <div>)" |> EmailBody.compose()
"<div>Why did the chicken cross the road (and here is a rogue div: <div>)?</div>"
The rogue <div> in the template parameter is safely escaped in the resulting string.







