Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render html template in javascript template in Phoenix Framework

Let's say I have 2 files, create.js.eex and post.html.eex and I want to render the contents of the post.html.eex template inside the create.js.eex template. Something like this:

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

The example above doesn't work because I need to escape quotes and other things in the string that gets returned and I can't find a way to do it

like image 356
NoDisplayName Avatar asked Jun 26 '26 06:06

NoDisplayName


1 Answers

You can use render_to_string

    Phoenix.View.render_to_string(MyApp.PageView, "index.html", foo: "bar")

Be aware that this can expose you to XSS.

like image 94
Preston Marshall Avatar answered Jun 29 '26 15:06

Preston Marshall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!