Crowdhailer

Crowdhailer

Creator of Raxx

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

Most Liked

Crowdhailer

Crowdhailer

Creator of Raxx

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.

Where Next?

Popular in Libraries Top

Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 13801 100
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. Features Unstyled Phoenix components. Storybook that can be added to...
New
markmark206
simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, product...
New
blatyo
https://www.conduitframework.com/ The best overview for how things are tied together is this presentation. Modules and functions are pre...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
Eiji
ExApi is a library that I’m developing now and hope release soon This library will allow to: list all apis list all api implementation...
New
Antrater
Hi everyone! I’m thrilled to announce a huge thing. We have been developing Elixir Moon Design System for quite a while. We are finally ...
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

Sub Categories:

We're in Beta

About us Mission Statement