defmodule UltraPlug.Render do @moduledoc """ Renders the template. With status :no_content, send an empty response. ## 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". """ 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, :skip), template: Keyword.get(opts, :template, "index.html"), } end def call(conn, opts \\ []) def call(conn = %{assigns: %{error: error}}, _) do {:error, error, conn} end def call(conn, %{status: :no_content}) do send_resp(conn, :no_content, "") end def call(conn, opts = %{status: status}) when status != :skip do conn |> put_status(status) |> call(%{opts | status: :skip}) end def call(conn, %{assigns: assigns, render: render, template: template}) do render.(conn, template, assigns) end def call(conn, opts) when is_list(opts) do call(conn, init(opts)) end end