ykostov

ykostov

Hello,

Little beginner post here, but I really can’t completely understand the usage of live.html.heex. I will start with what I want to achievе: I want to connect a LiveView socket to my root.html.heex. As I understand from the docs, for this reason live.html.heex is there.

To use the live layout, update your LiveView to pass the :layout option to use Phoenix.LiveView

it might seem obvious to you, but i really can’t figure it out where to update that LiveView, the router or something else?

For instance, if I create a page/index.html.heex I can connect it to its LiveView in the router:

live "/index", IndexLive

then, in IndexLive

defmodule MyAppWeb.IndexLive do
  use MyAppWeb, :live_view

  def render(assigns) do
   render PageView (or smth), "index.html", assigns
  end


  def mount(params, session, socket) do
(...)

However, this is not the case with live.html.heex.
I want to learn it, don’t what it all ready. Can you recommend me some links to read for this topic, because I really can’t find more in-depth explanations. (there surely is, i just cant find them)

forgot to mention the version used: phoenix 1.6.6

Showing Posts 1 to 2

cmo

cmo

It is worthwhile having a play with the generators to see what code they generate.

IIRC, live.html.heex was just a optional template that wrapped the liveview. That is, root.html.heex was at the top, then live.html.heex inside of it, then your page inside of that.

This is not the case anymore as live.html.heex and app.html.heex were merged in later versions.

The code lives in MyAppWeb these days, but looks like it might be in MyAppWeb.LayoutView in older versions.

sodapopcan

sodapopcan

Have a look in your lib/my_app_web.ex at the def live_view line. It seems you are using an older version of Phoenix as live.html.heex is no longer generated in favour of app.html.heex but it’s the still the same idea. Look in that file for this:

  def live_view do
    quote do
      use Phoenix.LiveView,
        layout: {MyAppWeb.Layouts, "live.html"} # These days it would just be `:live` as the the `.html.heex` is inferred

      unquote(html_helpers())
    end
  end

This is what gets called when you do use MyAppWeb, :live_view and is using live.html.heex for the layout. The layout itself is not a LiveView, just the layout template though you can set assigns for the layout in any LiveView that uses it.

If you wanted to have different layouts for different LiveViews you could create another layout in the layouts/ directory and then use it like so:

defmodule AppAppWeb.SomethingLive do
  use LiveView, layout: {MyAppWeb.Layouts, "admin.html"}
end

Of course it would be better to define a new helper function in the MyAppWeb module as the above won’t include the html_helpers/0:

  def admin_live do
    quote do
      use Phoenix.LiveView,
        layout: {MyAppWeb.Layouts, "admin.html"}

      unquote(html_helpers())
    end
  end

then:

defmodule MyAppWeb.Admin.HomeLive do
  use MyAppWeb, :admin_live
end

Really the best resource for learning this stuff is the official guides. Make sure you look at both Phoenix and LiveView docs and be sure to check out the “GUIDES” tab in each (The MODULES section is more for reference).

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews