2
votes

Following along with this Jose Valim post, I end up with a nested tuple of HTML information from the call to #content_tag.

{:safe,
 [60, "div", " class=\"form-group \"", 62,
  [[60, "label", " class=\"control-label\" for=\"_username\"", 62, "Username",
    60, 47, "label", 62],
   [60, "textarea",
    " class=\"form-control\" id=\"_username\" name=\"[username]\"", 62,
    ["\n", "username"], 60, 47, "textarea", 62], ""], 60, 47, "div", 62]}

I would like to write a test to check the output, but I would rather check the generated HTML than the nested tuples. How can I render this result into HTML?

1

1 Answers

4
votes

You can use Phoenix.HTML.Safe.to_iodata and IO.iodata_to_binary to convert that to a String:

iex(1)> raw = {:safe,
...(1)>  [60, "div", " class=\"form-group \"", 62,
...(1)>   [[60, "label", " class=\"control-label\" for=\"_username\"", 62, "Username",
...(1)>     60, 47, "label", 62],
...(1)>    [60, "textarea",
...(1)>     " class=\"form-control\" id=\"_username\" name=\"[username]\"", 62,
...(1)>     ["\n", "username"], 60, 47, "textarea", 62], ""], 60, 47, "div", 62]}
iex(2)> raw |> Phoenix.HTML.Safe.to_iodata |> IO.iodata_to_binary |> IO.puts
<div class="form-group "><label class="control-label" for="_username">Username</label><textarea class="form-control" id="_username" name="[username]">
username</textarea></div>