View Source Orbit.Controller (Orbit v0.1.0)
Process requests and render responses.
options
Options
:view
- a view module used to render actions
usage
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. If the :view
option has been specified, then the view is
automatically set by calling a function on the view module with the same name as the controller action. Finally,
action/2
is called.
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
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.
Puts Gemtext content as the body of a successful response.
Returns a list of all layouts.
Removes the innermost layout view.
Adds a nested layout view.
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.
Gets the Gemtext view to be rendered.
Link to this section Functions
Removes all layout views.
Puts Gemtext content as the body of a successful response.
Returns a list of all layouts.
Removes the innermost layout view.
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}
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.
options
Options
:mime_type
- the MIME type of the file; if unspecified, it is determined from the file extension
Gets the Gemtext view to be rendered.