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”.

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.

Last Post!

grych

grych

Creator of Drab

Thanks for the explanation. I am going to check it!
Thanks.

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement