How you look to add functions to controller?
@doc """
Return boolean conn has flashes
"""
def has_flash?(conn) do
get_flash(conn) !== %{}
end
@doc """
Return boolean conn has flash by key
"""
def has_flash?(conn, key) do
get_flash(conn) |> Map.has_key?(flash_key(key))
end
and add
# Import convenience functions from controllers
import Phoenix.Controller, only: [
get_csrf_token: 0, has_flash?: 1,
get_flash: 2, view_module: 1
]
Using like this
<% if has_flash?(@conn) do %>
<p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
<p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>
<% end %>
II am not sure it would help you with anything given that checking if there is a flash message does not guarantee get_flash(@conn, :info) will return something.
<% if has_flash?(@conn, :info) do %>
<p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
<% end %>
May be like this?
Which then I would just write as:
<% if info = get_flash(@conn, :info) do %>
<p class="alert alert-info" role="alert"><%= info %></p>
<% end %>
:)
+1!! It is a great idea!
@josevalim's solution didn't work for me at first, and then I realized you need to eval the if expression. Here's the updated version that works:
<%= if info = get_flash(@conn, :info) do %>
<p class="alert alert-info" role="alert"><%= info %></p>
<% end %>
@derekhubbard I think answer of @cpgo go to help you.
https://elixirforum.com/t/check-for-error-and-info-alert-in-phoenix/1984/3
Most helpful comment
Which then I would just write as:
:)