Read a partial template and return from an action of a controller

I have a partial template, say, it’s a table. I can render it in a template by calling “render”.

Is there a way to read its content and return it from an action of a controller as well? I need this for an ajax response.

There is Phoenix.View.render_to_string/3 that will give you the string value of a template. You call it just like you would Phoenix.View.render/3:

Phoenix.View.render_to_string(MyApp.LayoutView, "table.html", [rows: rows])

https://hexdocs.pm/phoenix/Phoenix.View.html#render_to_string/3

1 Like