Elixir 如何在Phoenix框架中用javascript模板呈现html模板

Elixir 如何在Phoenix框架中用javascript模板呈现html模板,elixir,phoenix-framework,Elixir,Phoenix Framework,假设我有两个文件,create.js.eex和post.html.eex,我想在create.js.eex模板中呈现post.html.eex模板的内容。大概是这样的: $("#something").append("<%= safe_to_string render "post.html", post: @post %>"); $(“#某物”)。追加(“”); 上面的示例不起作用,因为我需要对返回的字符串中的引号和其他内容进行转义,但我找不到方法来执行此操作您可以使用rende

假设我有两个文件,
create.js.eex
post.html.eex
,我想在
create.js.eex
模板中呈现
post.html.eex
模板的内容。大概是这样的:

$("#something").append("<%= safe_to_string render "post.html", post: @post %>");
$(“#某物”)。追加(“”);

上面的示例不起作用,因为我需要对返回的字符串中的引号和其他内容进行转义,但我找不到方法来执行此操作

您可以使用
render\u to\u string

    Phoenix.View.render_to_string(MyApp.PageView, "index.html", foo: "bar")
请注意,这会使您暴露于XSS。

使用escape\u javascript:

$("#something").append("<%= escape_javascript render("post.html", post: @post) %>");
$(“#某物”)。追加(“”);
您可以将_呈现为_字符串并对其进行转义,但似乎不需要太多,因为它返回一个字符串,所以它将对所有标记进行HTML转义

实际上,文档中有一个确切的例子:


哈哈,我真的很喜欢这个功能