How to pass @conn to your template with LiveView

I am very new to the Phoenix framework. I have done some reading on how this is set up and I am testing LiveView for a new project we have.
What I am trying to accomplish is to pass conn to my template on mount. This is required because I want to have a navigation bar for this specific template and I want to add an image on that nav bar.
When i try to call the template I get the error " assign @conn not available in eex template."
There is a reference of @conn on the template like so: “<img src=”<%= Routes.static_path(@conn, “/images/pic.png”) %>""
The LiveView guides found here Live layouts — Phoenix LiveView v0.15.7 mention: " The “root” layout is shared by both “app” and “live” layouts. It is rendered only on the initial request and therefore it has access to the @conn assign." Is there any way to grant access to conn to another template? If not can we pass some variable from conn to another template and how would we go about doing that.
I hope that this question makes some sense.

Do you mean to be passing the @socket? In your actual Live Views, you use the socket in place of where you would pass the conn in your root layouts and “dead views”.

So, your image source in your Live View would look like:

<img src="<%= Routes.static_path(@socket, "/images/pic.png") %>"

I was very quick to ask this question. Apparently this has been answered here: Use Routes.static_path in LiveComponent - #3 by LostKobrakai
Thank you very much for taking the time to look at this and clarifying this for me.

1 Like