Raxx.View - safe, fast HTML templates for web applications

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

Example

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
  • If :template is left unspecified the view will assume the template is in a file of the same name but with extension .html.eex in place of .ex or .exs.
  • An option for :layout can be omitted if all the content is in the view.
  • The :arguments can be set to [:assigns] if you prefer to use @var in you eex templates.
    This will not give you a compile time waring about unused arguments.

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")
  • If :template is left unspecified the partial will assume the template is in a file with the same name as the partial with extension .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
  • If :layout is left unspecified the layout will assume the template is in a file of the same name but with extension .html.eex in place of .ex or .exs.
  • All functions defined in a layout will be available in the derived views.

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
6 Likes

0.1.5 Adds optional variables to templates.

For example, with the following template:

<h1>
  <%= title %>
</h1>
<p>
  Hello <%= user.name %>
</p>

home_page.html.eex

And corresponding view module.

defmodule MyApp.HomePage do
  use Raxx.View,
    arguments [:user], 
    optional: [title: "MyApp"]
end

This view can then be used to render responses, as follows

user = %{name: "Anne"}
response = Raxx.response(:ok)

# render with default title
MyApp.HomePage.render(response, user)
# render with specific title
MyApp.HomePage.render(response, user, title: "MySpecificPage")

Layouts

Optional variables can be set on both Views and reusable Layouts.
Any optional variable default in a layout can be overridden by a View derived from it.

1.0 Roadmap

There is now a 1.0 Roadmap for Raxx.View as part of the Effort to stabilise Raxx and the ecosystem.

1 Like