View Source Orbit.Controller (Orbit v0.3.0)
Process requests and render responses.
Options
None.
Usage
The use Orbit.Controller
macro injects the following into the module:
@behaviour Orbit.Pipe
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
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
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 template based on the controller action name.
view MyApp.MyView
def index(req, _params) do
render(req) # => MyApp.MyView.index(req.assigns)
end
Layouts
Layouts are simply templates that are provided an @inner_content
assign which contains the content of the child view
to render within the layout.
def outer_layout(assigns) do
~G"""
begin outer
<%= @inner_content %>
end outer
"""
end
req
|> put_layout(&outer_layout/1)
|> render()
# =>
"""
begin outer
...view...
end outer
"""
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
Summary
Functions
Returns a list of all layouts.
Gets the Gemtext template to be rendered.
Puts Gemtext content as the body of a successful response.
Define a pipe to run in the controller prior to the action.
Sets the layout view.
Puts the Gemtext view to be rendered.
Renders the Gemtext view and layouts as a successful response.
Sends a file as a binary stream.
Sets the view module for rendering controller actions.
Functions
Returns a list of all layouts.
Gets the Gemtext template to be rendered.
Puts Gemtext content as the body of a successful response.
Define a pipe to run in the controller prior to the action.
Sets the layout view.
Layouts receive an @inner_content
assign that contains the content of the child view to render within the layout.
This is typically used directly in a router as a pipe, e.g.
pipe &Orbit.Controller.put_layout/2, &MyApp.LayoutView.main/1}
Puts the Gemtext view to be rendered.
Renders the Gemtext view and layouts as a successful response.
Sends a file as a binary stream.
Options
:mime_type
- the MIME type of the file; if unspecified, it is determined from the file extension
Sets the view module for rendering controller actions.
The template is based on the controller action name.