在Phoenix HTML Link中格式化特殊字符

时间:2017-10-26 18:01:22

标签: elixir phoenix-framework

我有一个特殊用例,我需要格式化Phoenix HTML链接。链接可以包含:*等特殊字符。举个例子 - CSVLOAD:*out:domain.org:call:4471

使用传统的Phoenix link生成/CSVLOAD%3A%2Aout%3Adomain.org%3Acall%3A4471作为网址。

请问我如何更正/CSVLOAD:*out:domain.org:call:4471

我的router.ex

resources "/instances", InstanceController, except: [:index] do
    resources "/rating-profiles", Rater.RatingProfileController do
    end
end

1 个答案:

答案 0 :(得分:1)

看起来Phoenix的帮助程序函数过于激进而无法转义HTML,它们也会逃脱EConvertError:

/

Phoenix Views中的iex(1)> post_path(MyApp.Endpoint, :show, "/CSVLOAD:*out:domain.org:call:4471") "/posts/%2FCSVLOAD%3A%2Aout%3Adomain.org%3Acall%3A4471" 功能并不那么激进:

link

我能想到两种解决方法:

  1. 手动构建URL并将其传递给iex(2)> link("Foo", to: "/CSVLOAD:*out:domain.org:call:4471") |> safe_to_string |> IO.puts <a href="/CSVLOAD:*out:domain.org:call:4471">Foo</a> ,如上所示,而不是使用路由器帮助函数,如link

  2. 删除非严格要求的转义符,并将其传递给post_path

    link

    您可以为此声明一个辅助函数,例如

    link "Foo", to: (post_path(...) |> URI.decode |> URI.encode)
    

    然后使用它:

    def reencode(string), do: string |> URI.decode |> URI.encode