tadasajon

tadasajon

How do I output JSON in a *.html.heex template? How do I get around Phoenix.HTML.Safe?

I have a template file that looks like the following:

<h1> The title: <%= @title  %> </h1>
<%= Jason.encode(@my_data) %>

But Phoenix is complaining, saying

protocol Phoenix.HTML.Safe not implemented for {:ok, "[{\"properties\":{\"gridProperties\":{\"etc

Apparently I can’t just output a JSON string of data into my html template?

How can I tell Phoenix that this data is safe to encode as JSON and display?

Most Liked

f0rest8

f0rest8

I just quickly tested this with @kokolegorille’s suggestion for Jason.encode!/2:

<%= Jason.encode!(%{a: 1}) %> and it outputs the tuple straight into the HTML as a string.

Jason.encode/2 throws the protocol undefined error. My to_string/1 suggestion doesn’t work for the similar reason: Jason.encode/1 returns the {:ok, String.t()} tuple whereas Jason.encode!/2 returns the String.t().

So, you would need to use Jason.encode!/2 or more specifically <%= Jason.encode!(@my_data) %> as koko suggests and it should work just fine.

f0rest8

f0rest8

Just from the error message, it sounds like you’re passing the tuple {:ok, string} and perhaps you need to just pass the string.

I haven’t tested or really familiar but looks like the inputs it has a protocol for are here in the docs.

Then it’ll return either the {:ok, String.t()} or the error tuple.

Edit

I realize the error message is cause you’re passing the :ok tuple to the template rather than a string.

Ah I think you might want to use a helper function or similar so you can clean the return you’re getting to be the string you want to stick in there.

You could even just write <%= to_string(Jason.encode(@mydata)) %> if you want to show the tuple.

Not totally sure what you’re asking, again, but if you want to render the json inside your html then you might want to check out how livebook approaches rendering different outputs inside the “cell” of the html page to draw inspiration.

kokolegorille

kokolegorille

Maybe like this?

<pre><%= Jason.encode!(@my_data) %></pre>

Note the exclamation mark.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
New

Other popular topics Top

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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement