Phoenix:将content_tag呈现为HTML?

时间:2016-10-26 20:49:20

标签: elixir phoenix-framework

在发布此Jose Valim帖后,我最终得到了来自#content_tag的调用的嵌套HTML信息元组。

{: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]}

我想编写一个测试来检查输出,但我宁愿检查生成的HTML而不是嵌套的元组。如何将此结果呈现为HTML?

1 个答案:

答案 0 :(得分:4)

您可以使用Phoenix.HTML.Safe.to_iodataIO.iodata_to_binary将其转换为字符串:

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>