如何将html添加到phoenixframework flash消息中?

时间:2017-03-20 20:38:35

标签: elixir phoenix-framework

我在控制器中尝试了以下内容:

conn
|> put_flash(:info, "text with <a href=\"link\">twitter<\\a>"
|> render "index.html"

我正在显示这样的消息(app.html.slim with bootstrap 4):

.alert.alert-success role="alert"= get_flash(@conn, :success)
.alert.alert-info role="alert"= get_flash(@conn, :info)
.alert.alert-warning role="alert"= get_flash(@conn, :warning)
.alert.alert-danger role="alert"= get_flash(@conn, :error)

也许有选择权? (尽管documentation另有建议)

编辑:生成的html是:

<div class="alert alert-info" role="alert">text with &lt;a href="link"&gt;twitter&lt;\a&gt;</div>

1 个答案:

答案 0 :(得分:6)

这是我在项目中所做的:

defmodule MyApp.PageController do
  use MyApp.Web, :controller
  import Phoenix.HTML.Link

  def index(conn, _params) do
    conn
    |> put_flash(:info, ["Please, visit ", link("Twitter", to: "http://twitter.com"), "!"])
    |> render("index.html")
  end
end

请注意,当其中一个元素是IO list的结果时,Flash消息内容不是普通字符串,而是评估者Phoenix.HTML.Link.link/2

希望有所帮助!