bluejack

bluejack

Setting assigns for layout-level component is shadowed by ... something?

What is defeating me today:

I want a component, included from a layout template, to have access to conn and/or assigns

What I am doing:

  1. Add a plug to the browser pipeline that takes information from the conn session and puts it into the assigns map.
    user = get_session(conn, :current_user)
    conn = assign(conn, :avatar, user.avatar)
    conn = assign(conn, :userid, user.id)

So far so good; if I then access this from a controller-driven template, we’re happy campers.

However, in components included from a layout template, assigns does NOT contain these values, and instead contains only %{__changed__: nil}

Sequentially, via logging, I can see that:

  1. The plug runs, and assigns the values, which are viewable in the conn struct.
  2. They layout component renders, but assigns at this point has the __changed__ value only
  3. The controller renders, at which point assigns is back to what I expected it to be.

So: something is shadowing assigns for the layout/component sequence? Where does that come from?

I presume this is a bug in my code, but I am not even sure where to look for it.

UPDATE – none of this is for Live View; this is all layout / controller driven content. Much of the prior conversations I have been able to find pertain to debugging live view interactions.

UPDATE – so, I have come to understand that “assigns” for a component is only what is directly passed in from the parent template:

     <.account_widget />

Which is implemented by a module with method:

  def account_widget(assigns) do

    ~H"""
    <div><%= @avatar %></div>
    """
  end

Only gets paramaters passed into it from the dot-notation, which in this case does not include @avatar – even though that data IS on the assigns portion of the conn struct.

So!

I can either pass the specific data I want down the “call stack” of template inclusion, from the point where it IS available – UGLY! –

OR

I can obtain the conn object from the component module? Is this possible? It certainly seems like it should be, but it’s not obvious how. Simply importing Plug.Conn does not seem sufficient. Thoughts on best practice here? This doesn’t seem like it should be such a difficult scenario, but other than doing the ugly thing, I’m a bit baffled.

UPDATE #3

So, I sort of successfully implemented the ugly approach, but it gets uglier when an attribute is not available: passing a nil value down the inclusion chain does not pass the compiler test.

Some of the components in the chain are pure templates, no explicit module in which to handle alternate attribute values or set defaults.

So, not only does it make the component NOT self contained, but it would force all kinds of hinky logic up the inclusion chain.

There MUST be a way to access the conn struct from a component! Or else I am really using this framework in the wrong way.

Marked As Solved Switch mode

cmo

cmo

This is the way.

Also Liked

msmithstubbs

msmithstubbs

I understand the feeling. I think there can be some adjustment required if you’re coming from different frameworks. But stick with it if you can. Hopefully it will start to make sense and you’ll come to love it :grin:

Hypothetically you could just pass the whole conn struct around everywhere and each component consumes it and does whatever it wants. The drawback I find with that is that at some point you might change what’s in the conn, or how it’s structured, and then a bunch of components all over the place break.

You’re probably already aware of it but I have found it useful to deal with nil values by conditionally rendering a component with the :if directive. For example, if the account widget should only render if the @avatar value is available:

<.account_widget avatar={@avatar} :if={assigns[:avatar]} />

Last Post!

bluejack

bluejack

Thanks for the additional tips @msmithstubbs Matt. Absolutely: I prefer to use a framework as it is intended to be used. Makes everyone’s lives easier.

I’m particularly grateful for the tip on :if – I have used that on standard elements, and (doh) it didn’t occur to me it could also be used on components. Ultimately not what I want to do in this instance, but definitely a useful tool!

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement