Continuing this thread How to replace “direct/simple” sigil_E to new sigil_H to avoid warnings?, to replace all sigil_E for the new sigil_H.
What is the best approach to interpolate sigil_H (inside other sigil_H parts)?
Is there a simple way to build html in parts with sigil_H?
It’s realy strange the whole assigns requirement for anything other than “new liveview render” or sigil_E be deprecated at all.
sigil_E (example)
import Phoenix.HTML
def breadcrumb(list) do
list_count =
list
|> Enum.count
|> Kernel.-(1)
li =
list
|> Enum.with_index()
|> Enum.map(fn {a, b} ->
text =
Translate.translate(a, false)
case b == list_count do
true ->
~E"""
<li class="breadcrumb-item active"><%= text %></li>
"""
false ->
~E"""
<li class="breadcrumb-item"><%= text %></li>
"""
end
end)
_html =
~E"""
<ol class="breadcrumb">
<%= li %>
</ol>
"""
end
sigil_H (example)
use Phoenix.Component
import Phoenix.HTML, only: [raw: 1]
def breadcrumb(list) do
assigns =
%{}
list_count =
list
|> Enum.count
|> Kernel.-(1)
li =
list
|> Enum.with_index()
|> Enum.map(fn {a, b} ->
text =
Translate.translate(a, false)
case b == list_count do
true ->
~H"""
<li class="breadcrumb-item active"><%= text %></li>
"""
false ->
~H"""
<li class="breadcrumb-item"><%= text %></li>
"""
end
|> Phoenix.HTML.Safe.to_iodata()
|> List.to_string()
end)
|> List.to_string()
_html =
~H"""
<ol class="breadcrumb">
<%= raw(li) %>
</ol>
"""
end
Expected result (in both cases)
<ol class="breadcrumb">
<li class="breadcrumb-item">Resultados</li>
<li class="breadcrumb-item">Painéis</li>
<li class="breadcrumb-item active">Principal</li>
</ol>