Skip to content

Instantly share code, notes, and snippets.

@magynhard
Last active January 12, 2017 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magynhard/be61a8753362300c8634dda422d99609 to your computer and use it in GitHub Desktop.
Save magynhard/be61a8753362300c8634dda422d99609 to your computer and use it in GitHub Desktop.
Elixir Phoenix Framework: Add class to active navigation entry
...
<div class="container">
<%= render "top_navi.html", conn: @conn %>
</div>
...
defmodule MyApp.LayoutView do
use MyApp.Web, :view
import MyApp.PageHelper
end
defmodule MyApp.PageHelper do
use MyApp.Web, :controller
def page_path?(conn, method) do
current_path = Path.join(["/" | conn.path_info])
cond do
page_path(conn, method) == current_path -> true
true -> false
end
end
end
...
<ul class="nav navbar-nav">
<li class="nav-item<%= if page_path?(@conn, :index) do %> active<% end %>">
<%= link "Listing", to: page_path(@conn, :index), class: "nav-link" %>
</li>
<li class="nav-item<%= if page_path?(@conn, :stats) do %> active<% end %>">
<%= link "Statistics", to: page_path(@conn, :stats), class: "nav-link" %>
</li>
</ul>
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment