Navigation
Generic parameters with query string
Read time: 2 minutes
This guide is a direct continuation of the previous guide
git clone https://github.com/adopt-liveview/v2-myapp.git --branch your-second-liveview-done.
The params variable passed to mount/3 is not limited to parameters in the URL path; it can also contain data coming from the query string. Let's create a simple LiveView in which if the user passes the query string like ?admin_mode=secret123, then they can see something just for admins. Update your PageLive to this:
defmodule MyappWeb.PageLive do
use MyappWeb, :live_view
def mount(params, _session, socket) do
admin? = params["admin_mode"] == "secret123"
socket = assign(socket, :admin?, admin?)
{:ok, socket}
end
def render(assigns) do
~H"""
<h1>Welcome to my Website!</h1>
<.link :if={@admin?} navigate={~p"/admin"}>Go to admin panel</.link>
"""
end
end
This LiveView reused several things covered in previous lessons. The main thing here is the fact that we received the params argument without specifying any specific param. If the user passes an empty query string, our system will simply leave the assign admin? as false. Open http://localhost:4000/?admin_mode=secret123 to see the admin panel message.
#Recap!
-
The
paramsvariable receives anything in the query string in key-value format like?x=10&y=12. -
As the
paramsvariable is a map, we can use theparams["key"]syntax to access optional values.
Feedback
Got any feedback about this page? Let us know!