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.
@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") %>
phoenix_html’s content_tag (which is used by link) might not work with attrs (like :class) as iolists since it would call nested_attrs on them. And the “performance win” would probably be completely negligible.
Afaik the biggest performance difference comes from not joining everything into really big binaries and modifying those. I doubt class names fall anywhere close into that category of “big”.