Phoenix Elixir Bootstrap alert flash helper
This is how I recently implemented a bootstrap alert helper for flash messages in a Phoenix app. It’s a pretty basic first solution that uses Elixir’s pattern matching to display the appropriate flash message type based on what’s currently set.
web/templates/layout/app.html.eex
<%= show_flash(@conn) %>
web/views/layout_view.ex
defmodule MyApp.LayoutView do
use MyApp.Web, :view
def show_flash(conn) do
get_flash(conn) |> flash_msg
end
def flash_msg(%{"info" => msg}) do
~E"<div class='alert alert-info'><%= msg %></div>"
end
def flash_msg(%{"error" => msg}) do
~E"<div class='alert alert-danger'><%= msg %></div>"
end
def flash_msg(_) do
nil
end
end
I am available for Elixir/Phoenix consulting work – get in touch to learn more.