gpt4 book ai didi

elixir - Phoenix : Render content_tag as HTML?

转载 作者:行者123 更新时间:2023-12-02 00:50:39 25 4
gpt4 key购买 nike

跟着这个 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?

最佳答案

您可以使用 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>

关于elixir - Phoenix : Render content_tag as HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40271865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com