~H is not LiveView specific, that’s sorta the whole point So you can go ahead and convert away in all your templates be they LiveView or Phoenix templates. The only thing you have to do (which can be tedious depending on the size of your app) is change all attribute values from <%= foo %> to {foo}.
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.
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.
Ah yes, that is a bit of Phoenix magic that is confusing at first.
It’s not actually true that assigns is unused. ~H is a macro that expects an outer assigns variable to be defined—that’s how the @ variable access works.
As a side note, you don’t need the = %{} part, def render(assigns) will do (well, unless you are using it in a function that isn’t called with the component syntax).
Sorry, I read your reply minutes after I woke up and didn’t read properly. I see now you were not asking why and realize you are probably dealing with .heex.html files.
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.