Navigation

Generic parameters with query string

Read time: 1 minute

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. Create and run query_string.exs:

Mix.install([
  {:liveview_playground, "~> 0.1.3"}
])

defmodule PageLive do
  use LiveviewPlaygroundWeb, :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

LiveviewPlayground.start()

This LiveView reused several things covered in previous classes. 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.

#Recap!

  • The params variable receives anything in the query string in key-value format like ?x=10&y=12.
  • As the params variable is a map, we can use the params["key"] syntax to access optional values.

Feedback

Got any feedback about this page? Let us know!