View Source Orbit.Controller (Orbit v0.2.1)

Process requests and render responses.

options

Options

None.

usage

Usage

The use Orbit.Controller macro injects the following functions into the module:

def call(request, arg)
def action(request, action) # overridable

The call/2 function implements the Orbit.Pipe callback, making the controller behave like any other pipe. The arg is the action name, as an atom, and is set to the :action assign. Any pipe/2 definitions in this controller are called first, and then action/2 is called.

overriding-action-2

Overriding action/2

The action/2 function is overridable. It's an easy way to extend the controller's default behavior, or to customize the signature of the action functions to something other than action_name(req, params). Its default implementation is:

def action(req, action) do
  apply(__MODULE__, action, [req, req.params])
end

views

Views

A controller can render responses directly in the controller action, or defer the rendering to an external view module. Define the view/1 pipe to set the view based on the controller action name.

  view MyApp.MyView

  def index(req, _params) do
    render(req) # => MyApp.MyView.index(req.assigns)
  end

layouts

Layouts

Layouts are simply views that are provided an @inner_content assign which contains the content of the child view to render within the layout. Layouts can be nested by pushing additional layouts to the Request.

def outer_layout(assigns) do
  ~G"""
  begin outer
  <%= @inner_content %>
  end outer
  """
end

def inner_layout(assigns) do
  ~G"""
  begin inner
  <%= @inner_content %>
  end inner
  """
end

req
|> push_layout(&outer_layout/1)
|> push_layout(&inner_layout/1)
|> render()

# =>
"""
begin outer
begin inner
...view...
end inner
end outer
"""

example

Example

# Router
route "/users", MyApp.UserController, :index
route "/users/:id", MyApp.UserController, :show

# Controller
defmodule MyApp.UserController do
  use Orbit.Controller

  view MyApp.UserView

  def index(req, _params), do: ...
  def show(req, %{"id" => id}), do: ...
end

Link to this section Summary

Functions

Removes all layout views.

Gets the Gemtext view to be rendered.

Puts Gemtext content as the body of a successful response.

Returns a list of all layouts.

Define a pipe to run in the controller prior to the action.

Removes the innermost layout view.

Adds a nested layout view.

Sets the response view based on the controller action name.

Puts a Gemtext view to be rendered if one has not already been set.

Puts the Gemtext view to be rendered.

Renders the Gemtext view and layouts as a successful response.

Sends a file as a binary stream.

Define the view module for rendering controller actions.

Link to this section Functions

Link to this function

clear_layouts(req, arg \\ [])

View Source

Removes all layout views.

Gets the Gemtext view to be rendered.

Puts Gemtext content as the body of a successful response.

Returns a list of all layouts.

Link to this macro

pipe(pipe, arg \\ [])

View Source (macro)

Define a pipe to run in the controller prior to the action.

Link to this function

pop_layout(req, arg \\ [])

View Source

Removes the innermost layout view.

Link to this function

push_layout(req, layout)

View Source

Adds a nested layout view.

Layouts are rendered outer-to-inner, so the first layout pushed onto the stack will be the outermost layout, and the next layout pushed will be nested inside that, and so on.

This is typically used directly in a router as a pipe, e.g.

pipe {Orbit.Controller, :push_layout}, {MyApp.LayoutView, :main}
Link to this function

put_action_view(req, view_module)

View Source

Sets the response view based on the controller action name.

The view rendered is [view_module].[action]/2.

Puts a Gemtext view to be rendered if one has not already been set.

Puts the Gemtext view to be rendered.

Renders the Gemtext view and layouts as a successful response.

Link to this function

send_file(req, path, opts \\ [])

View Source

Sends a file as a binary stream.

options

Options

  • :mime_type - the MIME type of the file; if unspecified, it is determined from the file extension
Link to this macro

view(view_module)

View Source (macro)

Define the view module for rendering controller actions.

This is a convenience wrapper that simply wraps Orbit.Controller.put_action_view/2 as a pipe/2 definition.