Zesky665

Zesky665

What is the ~E sigil in the Drab tutorial?

Hi,

I’m going through the examples on the drab page. In the ‘chat example’ section, in the code example.

defhandler update_chat(socket, sender) do
  nick = get_store(socket, :nickname, "Anonymous")
  html = ~E"<strong><%= nick %>:</strong> <%= message %><br>" |> safe_to_string()
  broadcast_insert socket, "#chat", :beforeend, html
  set_prop socket, this(sender), value: ""
  broadcast_js socket, "document.querySelector('#chat').scrollTop = 
  document.querySelector('#chat').scrollHeight"`
end  

On the third line, the one with html, a sigil ~E is used. I don’t know what this does, and it doesn’t show up in the elixir docs or in the page source code. Anyone know what this is?

Marked As Solved

yurko

yurko

That comes from Phoenix HTML package: “Provides ~E sigil with HTML safe EEx syntax inside source files”.

https://hexdocs.pm/phoenix_html/Phoenix.HTML.html#sigil_E/2

Also Liked

grych

grych

Creator of Drab

It is important to use safe when you build html from the outside source, like inputs. Future versions of Drab will take safe directly, so you’ll not have to add safe_to_string/1.

srowley

srowley

Yes and no. I did the steps (edited below to be more accurate) and I get the same error. However - this time I did rm -rf _build and then when I restarted the server it recompiled with no error. That works for me!

That said - please let me re-document my steps as I did not record them 100% correctly the first time. I am using Phoenix 1.4, Elixir 1.7.4, Erlang/OTP 21.

  1. mix phx.new my_app --no-ecto
  2. add {:drab, "~0.10.1"} to mix.exs
  3. mix deps.get
  4. mix drab.install
  5. mix drab.gen.commander Page
  6. add config: drab, :presence, true to config.exs
  7. add Drab.Presence to my supervision tree in lib/my_app/application.ex
  8. add import Drab.Presence to page_commander.ex
  9. note that config: Drab, MyAppWeb.Endpoint, otp_app: my_app is in config.exs
  10. run mix phx.server

I deviated from the instructions in the docs in two respects:

  1. The docs say to modify the supervision tree in lib/my_app_web.ex; I’m assuming that where the supervision tree was specified in an older version of Phoenix?

  2. The docs say to make sure that config: Drab, MyAppWeb.Endpoint, otp_app: my_app_web is set, not my_app, which is what was inserted into my file by the generator. I admit I don’t really understand what that is doing. I did modify it to my_app_web and get the same error, for what that is worth.

grych

grych

Creator of Drab

Do

import Phoenix.HTML

in the commander, or just call Phoenix.HTML.safe_to_string directly.

marat

marat

Tried to call module.function, that gave page_commander.ex:14: undefined function sigil_E/2.
Now,
page_commander.ex:16: undefined function message/0

it’s the 3rd line in

  defhandler update_chat(socket, sender) do
    nick = get_store(socket, :nickname, "Anonymous")
    html = ~E"<strong><%= nick %>:</strong> <%= message %><br>" |> safe_to_string()
grych

grych

Creator of Drab

Yes, I made a mistake when copying the code to the html, will be added soon.
message = sender["value"] is correct.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement