kattair0ta8
How to highlight a current link in a navbar? algorithm
Say, I have this:
<div class="navbar">
<a href="/" class="active">Home</a>
<a href="/link1">Link1</a>
<a href="/link2">Link2</a>
<a href="/link3">Link3</a>
</div>
What’s the best way to highlight the current link depending on the current url? For link3, it’ll lool like this:
<div class="navbar">
<a href="/" class="">Home</a>
<!--
or without attribute 'class' at all
<a href="/">Home</a>
-->
<a href="/link1">Link1</a>
<a href="/link2">Link2</a>
<a href="/link3" class="active">Link3</a>
</div>
I figure I need to create a dictionary and pass it to html. How would I go about this? I can’t figure out.
Most Liked
chensan
Here’s how I did it in my project.
First, define a function in layout_view.ex,
@doc """
Set css class for active link.
"""
@spec css_class(Plug.Conn.t(), String.t(), String.t(), String.t()) :: String.t()
def css_class(conn, path, defaultClass, activeClass \\ "active") do
if path == Phoenix.Controller.current_path(conn) do
defaultClass <> " " <> activeClass
else
defaultClass
end
end
Then in my app.html.eex call the function:
<%= link "Dashboard", to: Routes.user_path(@conn, :show, @current_user), class: css_class(@conn, Routes.user_path(@conn, :show, @current_user), "nav-link") %>
7
OvermindDL1
My navbar is a list of data entries (with some dropdown that are just further lists) and they define what is active or note, built up from the Views themselves so they know that they are being called or not and of which template. ![]()
1
enkr1
I have improved this to suit my case which having /action_name/slug/etc
@doc """
Sets additional css class when for the current page.
"""
def css_class(conn, path, default_class \\ nil, active_class \\ "active") do
if is_nav_item_actvie?(conn, path) do
"#{default_class} #{active_class}"
else
default_class
end
end
@doc """
Determine if the nav item is active or not.
"""
def is_nav_item_actvie?(conn, path) do
input_action_name = Enum.at(String.split(path, "/"), 1)
current_action_name = List.first(conn.path_info)
IO.inspect(%{c: current_action_name, p: input_action_name})
current_action_name === input_action_name
end
Just sharing~
1
Popular in Questions
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function:
-dialyzer...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Other popular topics
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition)
It’s been a while since we first asked this, I...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









