如何在Phoenix Framework中的javascript模板中呈现html模板

NoD*_*ame 2 elixir phoenix-framework

假设我有2个文件,create.js.eex并且post.html.eex我想在post.html.eex模板中呈现模板的内容create.js.eex.像这样的东西:

$("#something").append("<%= safe_to_string render "post.html", post: @post %>");
Run Code Online (Sandbox Code Playgroud)

上面的例子不起作用,因为我需要转义返回的字符串中的引号和其他东西,我找不到办法做到这一点

Pre*_*all 6

您可以使用 render_to_string

    Phoenix.View.render_to_string(MyApp.PageView, "index.html", foo: "bar")
Run Code Online (Sandbox Code Playgroud)

请注意,这可能会使您暴露于XSS.