<%= @conn.request_path %>
<%= Phoenix.Controller.current_path(@conn) %>
<div class="hidden md:block md:ml-10 md:pr-4 md:space-x-8 w-full menu">
<%= live_redirect "Home", to: "/", class: if @conn.request_path == "/", do: "active", else: "" %>
<%= live_redirect "Discover", to: "/discover", class: if @conn.request_path == "/discover", do: "active", else: "" %>
<%= live_redirect "About", to: "/about", class: if @conn.request_path == "/about", do: "active", else: "" %>
<%= live_redirect "Contact Us", to: "/contact", class: if @conn.request_path == "/contact", do: "active", else: "" %>
<%= live_redirect "Login", to: "/login", class: "float-right" %>
</div>
Hi, i want to highlight the menu item in navigation bar, but <%= @conn.request_path %> in the header partial doesn’t change at all between clicks, there are solutions in the forum. but i want to know why conn.request_path does’nt change at all. the phoenix_active_link also use conn.request_path.
@dfang your header and footer should be part of your live layout, not part of your static layout. Then they will be able to access assigns on the socket.
currently the header and footer is in root.html.heex, when i move it to live.html.heex , i got this error:
ArgumentError at GET /contact
assign @conn not available in template.
Please make sure all proper assigns have been set. If you are
calling a component, make sure you are passing all required
assigns as arguments.
Available assigns: [:__changed__, :flash, :inner_content, :live_action, :socket]
put render PragmaticWeb.SharedView, "header.html", assigns in live.html.leex not root.html.leex
use @socket.view instead of @conn.request_path to add active css class <%= live_redirect "Home", to: "/", class: if Kernel.inspect(@socket.view) == "DemoWeb.HomeLive", do: "active", else: "1" %>
It will show you how to use the lifecycle hooks to add an @active_tab assign that you will use to set the active class. Whether or not it is easier than Rails I do not know, but it is very flexible!