Render template outside of controller

I want to render a template outside of a controller and write the html out to a file. Can I use the Phoenix HTML Engine to do this?

Yes.

I guess you’re asking about example :slight_smile:

If you want to totally decouple your HTML from MyAppWeb directory:

defmodule MyModuleView do
  require EEx
  EEx.function_from_file(:def, :document, "document.html.eex", [:assigns], engine: Phoenix.HTML.Engine)
end
<html>
...
<%= @text %>
...
</html>
{:safe, iodata} = MyModuleView.document(%{text: "Hello world"})
File.write!("output.html", iodata)

If you just want to get HTML using existing phoenix infrastructure:

MyAppWeb.LayoutView.render("app.html", inner_content: MyApp.MyView.render("index.html", ...), conn: conn)
2 Likes

With phoenix 1.6 the view layer was even extracted to it’s own package to further strengthen the position of “this works everywhere not just with phoenix”

1 Like