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
That comes from Phoenix HTML package: “Provides ~E sigil with HTML safe EEx syntax inside source files”.
Also Liked
grych
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
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.
mix phx.new my_app --no-ecto- add
{:drab, "~0.10.1"}tomix.exs mix deps.getmix drab.installmix drab.gen.commander Page- add
config: drab, :presence, truetoconfig.exs - add
Drab.Presenceto my supervision tree inlib/my_app/application.ex - add
import Drab.Presencetopage_commander.ex - note that
config: Drab, MyAppWeb.Endpoint, otp_app: my_appis inconfig.exs - run
mix phx.server
I deviated from the instructions in the docs in two respects:
-
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? -
The docs say to make sure that
config: Drab, MyAppWeb.Endpoint, otp_app: my_app_webis set, notmy_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 tomy_app_weband get the same error, for what that is worth.
grych
Do
import Phoenix.HTML
in the commander, or just call Phoenix.HTML.safe_to_string directly.
Last Post!
grych
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









