I writing a little code generator and have the following question.
I have a variable containing the string: str = "Foo\nBar"
The string is read from a text file in this form not a literal declared in elixir.
If I have x = "<%= %>"
in my eex template I get
x = “Foo
Bar”
but I wont
x = “Foo\nBar”
It seems that raw is part of phoenix. For my lib it would be not planed to depend on phoenix. I checked the implementation but this gets translated to {:save, str} which is also not plain eex.
If you’re using HTML then this will apply for presenting your data with line breaks read by HTML inside of forms. (How to use the raw specified above^)
You can create new input helpers, editing only slightly what phoenix has in their accepted releases.
defmodule FulfillmentCart.Helpers.FormHelpers do
alias Phoenix.HTML.Form
import Phoenix.HTML
import Phoenix.HTML.Tag
def textarea(form, field, opts \\ []) do
opts =
opts
|> Keyword.put_new(:id, input_id(form, field))
|> Keyword.put_new(:name, input_name(form, field))
{value, opts} = Keyword.pop(opts, :value, input_value(form, field))
content_tag(:textarea, ["\n", html_escape(value || "")], opts)
end
end
This is source code copied from [Phoenix Master Branch].
Simply change the way your data is returned to the page from escaped data to raw data, like this:
content_tag(:textarea, ["\n", html_escape(value || "")], opts)
to content_tag(:textarea, ["\n", value |> raw, opts)
and implement the custom helper into your template just like you would any other textarea