Phoenix.HTML.Safe error when it tries to convert map to string

Hello, I want to take a Json and convert it to string in Phoenix,

my original Map:

render(conn, "index.html", breadcrumb: Blog.breadcrumb_creator(breadcrumb), categories_list: Jason.decode!(categories_list), allposts: Jason.encode!(allposts))


%{
  "outputs" => %{
    "categories" => %{
      "entries" => [
        %{
          "description" => "برای بهبود تمیز سازی تست های حاصله مجبور به ساخت یکی از دسته های این دیده می شود و برای همین باید تستی برای تایپ دیدنی انجام بدهیم",
          "group_acl" => "unactived",
          "language" => "en",
          "pic_x1_link" => "/ss/pps",
          "pic_x2_link" => "/ss/pps",
          "pic_x3_link" => "/ss/pps",
          "record_count" => 2,
          "seo_alias_link" => "shahryar1-category",
          "seo_description" => "بیایید تمام بدن هایمان را برای سئو آماده کنیم تا زیبایی در چشمان باشد",
          "seo_language" => "fa",
          "seo_language_link" => "/ss/pps",
          "seo_words" => "sport,yhink",
          "status" => true,
          "title" => "the end of test"
        },
        %{
          "description" => "برای بهبود تمیز سازی تست های حاصله مجبور به ساخت یکی از دسته های این دیده می شود و برای همین باید تستی برای تایپ دیدنی انجام بدهیم",
          "group_acl" => "unactived",
          "language" => "en",
          "pic_x1_link" => "/ss/pps",
          "pic_x2_link" => "/ss/pps",
          "pic_x3_link" => "/ss/pps",
          "record_count" => 1,
          "seo_alias_link" => "shahryar1-c1ategory",
          "seo_description" => "بیایید تمام بدن هایمان را برای سئو آماده کنیم تا زیبایی در چشمان باشد",
          "seo_language" => "fa",
          "seo_language_link" => "/ss/pps",
          "seo_words" => "sport,yhink",
          "status" => true,
          "title" => "1-the end of test"
        }
      ],
      "page_number" => 1,
      "page_size" => 5,
      "total_entries" => 2,
      "total_pages" => 1
    }
  }
}

I have loaded this in the template like this :

<%= %{"outputs" => %{"categories" => %{"entries" => entries}}} = @categories_list %>

        <%= for el <- entries do %>
          <%= el["title"] %>
        <% end %>

but I have the Phoenix.HTML.Safe error:

[error] #PID<0.521.0> running TrangellHtmlSiteWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:9991 (http)
Request: GET /blog
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol Phoenix.HTML.Safe not implemented for %{"outputs" => %{"categories" => %{"entries" => [%{"description" => "برای بهبود تمیز سازی تست های حاصله مجبور به ساخت یکی از دسته های این دیده می شود و برای همین باید تستی برای تایپ دیدنی انجام بدهیم", "group_acl" => "unactived", "language" => "en", "pic_x1_link" => "/ss/pps", "pic_x2_link" => "/ss/pps", "pic_x3_link" => "/ss/pps", "record_count" => 2, "seo_alias_link" => "shahryar1-category", "seo_description" => "بیایید تمام بدن هایمان را برای سئو آماده کنیم تا زیبایی در چشمان باشد", "seo_language" => "fa", "seo_language_link" => "/ss/pps", "seo_words" => "sport,yhink", "status" => true, "title" => "the end of test"}, %{"description" => "برای بهبود تمیز سازی تست های حاصله مجبور به ساخت یکی از دسته های این دیده می شود و برای همین باید تستی برای تایپ دیدنی انجام بدهیم", "group_acl" => "unactived", "language" => "en", "pic_x1_link" => "/ss/pps", "pic_x2_link" => "/ss/pps", "pic_x3_link" => "/ss/pps", "record_count" => 1, "seo_alias_link" => "shahryar1-c1ategory", "seo_description" => "بیایید تمام بدن هایمان را برای سئو آماده کنیم تا زیبایی در چشمان باشد", "seo_language" => "fa", "seo_language_link" => "/ss/pps", "seo_words" => "sport,yhink", "status" => true, "title" => "1-the end of test"}], "page_number" => 1, "page_size" => 5, "total_entries" => 2, "total_pages" => 1}}}. This protocol is implemented for: Atom, BitString, Date, DateTime, Float, Integer, List, NaiveDateTime, Time, Tuple
        (phoenix_html) /Applications/MAMP/htdocs/elixir-ex-source/Trangell_Main/trangell_html_site_umbrella/deps/phoenix_html/lib/phoenix_html/safe.ex:1: Phoenix.HTML.Safe.impl_for!/1
        (phoenix_html) /Applications/MAMP/htdocs/elixir-ex-source/Trangell_Main/trangell_html_site_umbrella/deps/phoenix_html/lib/phoenix_html/safe.ex:15: Phoenix.HTML.Safe.to_iodata/1
        (trangell_html_site_web) lib/trangell_html_site_web/templates/blog/index.html.eex:183: TrangellHtmlSiteWeb.BlogView."index.html"/1
        (trangell_html_site_web) lib/trangell_html_site_web/templates/layout/app.html.eex:26: TrangellHtmlSiteWeb.LayoutView."app.html"/1
        (phoenix) lib/phoenix/view.ex:332: Phoenix.View.render_to_iodata/3
        (phoenix) lib/phoenix/controller.ex:740: Phoenix.Controller.do_render/4
        (trangell_html_site_web) lib/trangell_html_site_web/controllers/blog_controller.ex:1: TrangellHtmlSiteWeb.BlogController.action/2
        (trangell_html_site_web) lib/trangell_html_site_web/controllers/blog_controller.ex:1: TrangellHtmlSiteWeb.BlogController.phoenix_controller_pipeline/2
        (trangell_html_site_web) lib/trangell_html_site_web/endpoint.ex:1: TrangellHtmlSiteWeb.Endpoint.instrument/4
        (phoenix) lib/phoenix/router.ex:278: Phoenix.Router.__call__/1
        (trangell_html_site_web) lib/trangell_html_site_web/endpoint.ex:1: TrangellHtmlSiteWeb.Endpoint.plug_builder_call/2
        (trangell_html_site_web) lib/trangell_html_site_web/endpoint.ex:1: TrangellHtmlSiteWeb.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:16: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /Applications/MAMP/htdocs/elixir-ex-source/Trangell_Main/trangell_html_site_umbrella/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

How can I fix this and use my data in the Html template?

I fix this I used <% instead of <%= in <%= %{"outputs" => %{"categories" => %{"entries" => entries}}} = @categories_list %> and it was fixed.

1 Like

Yep, <% just runs code where <%= takes the result of the code and outputs it to the html (hence why it must be an iostring or close enough via to_string/1). :slight_smile:

2 Likes