raxx_view v0.1.7 Raxx.View.Layout

Create a general template that can be reused by views.

Using this module will create a module that can be used as a view. All functions created in the layout module will be available in the layout template and the content template

Example

Creating a new layout

# www/layout.html.eex
<h1>My Site</h1>
<%= __content__ %>

# www/layout.ex
defmodule WWW.Layout do
  use Raxx.View.Layout,
    layout: "layout.html.eex"

  def format_datetime(datetime) do
    DateTime.to_iso8601(datetime)
  end
end

Creating a view

# www/show_user.html.eex
<h2><%= user.username %></h2>
<p>signed up at <%= format_datetime(user.interted_at) %></p>

# www/show_user.ex
defmodule WWW.ShowUser do
  use Raxx.SimpleServer
  use WWW.Layout,
    template: "show_user.html.eex",
    arguments: [:user]

  @impl Raxx.Server
  def handle_request(_request, _state) do
    user = # fetch user somehow

    response(:ok)
    |> render(user)
  end
end

Options

  • layout (optional): The eex file containing the layout template. If not given the template file will be generated from the file of the calling module. i.e. path/to/file.ex -> path/to/file.html.eex

  • imports (optional): A list of modules to import into the template. The default behaviour is to import only the layout module into each view. Set this option to false to import no functions.