Raxx.View

Generate HTML views from .eex template files for Raxx web applications.

Hex pmLicense

Defining Views

Template to show a list of users.

lib/my_app/layout.html.eex

<header>
<h1><% title %></h1>
<%= __content__ %>
</header>

lib/my_app/list_users.html.eex

<%= for user <- users do %>
<section>
<a href="/users/<%= user.id %>"><%= user.name %></a>
<p>
Joined on <%= Timex.format!(user.registered_at, "{YYYY}-0{M}-0{D}") %>
</p>
</section>

lib/my_app/list_users.ex

defmodule MyApp.ListUsers do
use Raxx.View,
arguments: [:users, :title],
template: "list_users.html.eex",
layout: "layout.html.eex"
end

Helpers

Helpers can be used to limit the amount of code written in a template. Functions defined in a view module, public or private, can be called in the template.

lib/my_app/list_users.ex

# ... rest of module
def display_date(datetime = %DateTime{}) do
Timex.format!(datetime, "{YYYY}-0{M}-0{D}")
end
def user_page_link(user) do
~E"""
<a href="/users/<%= user.id %>"><%= user.name %></a>
"""
end

Update the template to use the helpers.

lib/my_app/list_users.html.eex

<%= for user <- users do %>
<section>
user_page_link(user)
<p>
Joined on <%= display_date(user.registered_at) %>
</p>
</section>

Partials

A partial is like any another helper function, but one that uses an EEx template file.

lib/my_app/list_users.ex

# ... rest of module
partial(:profile_card, [:user], template: "profile_card.html.eex")

Update the template to make use of the profile_card helper

lib/my_app/list_users.html.eex

<%= for user <- users do %>
profile_card(user)
<% end %>

Reusable Layouts and Helpers

Layouts can be used to define views that share layouts and possibly helpers.

lib/my_app/layout.ex

defmodule MyApp.Layout do
use Raxx.View.Layout,
layout: "layout.html.eex"
def display_date(datetime = %DateTime{}) do
Timex.format!(datetime, "{YYYY}-0{M}-0{D}")
end
def user_page_link(user) do
~E"""
<a href="/users/<%= user.id %>"><%= user.name %></a>
"""
end
partial(:profile_card, [:user], template: "profile_card.html.eex")
end

The list users view can be derived from our layout and use the shared helpers.

lib/my_app/list_users.ex

defmodule MyApp.ListUsers do
use MyApp.Layout,
arguments: [:users, :title],
template: "list_users.html.eex",
end