Concat Sigil ~E and others (how to)

Hello Guys, Elixir/phoenix have some helper to concat sigils?
What do you think about this feature?

Code:

defmodule Teste do
  import Phoenix.HTML

  def sigil_concat_1() do
      ~E"""
      <div class="col-lg-4">
         <%= "Hello" %>
      </div>
      """
  end

  def sigil_concat_2() do
      world = "World"
      ~E"""
      <div class="col-lg-4">
         <%= world %>!
      </div>
      """
  end

end

Test:

Interactive Elixir (1.5.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> s1 = Teste.sigil_concat_1
{:safe, [[["" | "<div class=\"col-lg-4\">\n   "] | "Hello"] | "\n</div>\n"]}
iex(2)> s2 = Teste.sigil_concat_2
{:safe, [[["" | "<div class=\"col-lg-4\">\n   "] | "World"] | "!\n</div>\n"]}
iex(3)> s1 <> s2
** (ArgumentError) argument error

iex(3)> s1 ++ s2
** (ArgumentError) argument error
    :erlang.++({:safe, [[["" | "<div class=\"col-lg-4\">\n   "] | "Hello"] | "\n</div>\n"]}, {:safe, [[["" | "<div class=\"col-lg-4\">\n   "] | "World"] | "!\n</div>\n"]})
iex(3)> 
1 Like

sigil_e and sigil_E create some “safe” HTML strings. As long as you do further processing only in Phoenix.HTML one or more of the following should work:

e1 = ~E"stuff"
e2 = ~E"more stuff"

[e1|e2]

raw(safe_to_string(e1) <> safe_to_string(e2))

~e"#{e1}#{e2}"
3 Likes