如何渲染在另一个组件的参数内传递的组件

时间:2016-10-04 18:42:40

标签: elixir phoenix-framework

我想渲染一下:

<%= render MyProj.SnippetView, "doc_ready.html",
        state: "DOMContentLoaded",
        func: "setTimeout(function(){
                        #{render(MyProj.SnippetView, "scroll_page.html",
                            type: nil,
                            offset: -75,
                            target: @scrollTo,
                            duration: nil)}
                        }, 1)"
    %>

但我收到了错误:

protocol String.Chars not implemented for {:safe, [[[[[[[[["" | "var scrollOffset = "], "" | "-75"] | ";\nvar scrollTarget = "], [["" | "$(\"#"] | "imgasEmpresas16"] | "\").offset().top + scrollOffset"] | ";\n$('html, body').animate({scrollTop: scrollTarget"] | ""] | "}, \n "], "" | "1"] | ");"]}

是因为我不能这样做,还是因为我做错了?

1 个答案:

答案 0 :(得分:1)

{:safe, iodata}是Phoenix将标记字符串标记为可安全打印而无需转义<>&等字符以防止XSS的方法。您可以将{:safe, _}字词转换为iodata,可以使用Phoenix.HTML.Safe.to_iodata/1在字符串插值中使用:

<%= render MyProj.SnippetView, "doc_ready.html",
        state: "DOMContentLoaded",
        func: "setTimeout(function(){
                        #{render(MyProj.SnippetView, "scroll_page.html",
                            type: nil,
                            offset: -75,
                            target: @scrollTo,
                            duration: nil) |> Phoenix.HTML.Safe.to_iodata}
                        }, 1)"
%>