Current section
Files
Jump to
Current section
Files
lib/render.ex
defmodule UltraPlug.Render do
@moduledoc """
Renders the template.
## Options
- **assigns:** overrides conn.assigns in Phoenix.
- **render:** defaults to [Phoenix.Controller.render/3](https://hexdocs.pm/phoenix/Phoenix.Controller.html#render/3).
- **status:** calls [Plug.Conn.put_status/2](https://hexdocs.pm/plug/Plug.Conn.html#put_status/2) before rendering.
- **template:** defaults to "index.html".
"""
use UltraPlug, return_error_as_tuple: true
import Plug.Conn
def init(opts) do
%{
assigns: Keyword.get(opts, :assigns, %{}),
render: Keyword.get(opts, :render, &Phoenix.Controller.render/3),
status: Keyword.get(opts, :status, :none),
template: Keyword.get(opts, :template, "index.html")
}
end
def call(conn, opts = %{status: status}) when status != :none do
conn
|> put_status(status)
|> call(%{opts | status: :none})
end
def call(conn, %{assigns: assigns, render: render, template: template}) do
render.(conn, template, assigns)
end
end