AGURRU
Hi I don’t come from a front end background and have been trying to build some pages using HEEX templates.
I plan to create some selectable cards for a form. I had it working using the following code with simple formatting:
<%= for investment <- @investments do %>
<%= investment.investment_name %>
<% end %>
I feel link I have tried all the syntax possibilities under the sun to use the AlpineJS x-for functionality with the above HEEX templating code.
Can someone please show me how I can use the x-for functionality in place of the above?
Many Thanks,
Sam
Trending in Questions
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
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
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
msimonborg
Welcome @AGURRU! I think it would be helpful to know why you want to use Alpine.js to render a list of elements rather than just using the Elixir/EEX code you already have? The code you have is great and gets the job done. What you’re asking for is possible though. If
@investmentsis a list of maps, you will have to encode the whole list to JSON firstBut if
@investmentsis a list of Ecto schema structs, which is very likely if the data is coming from your database, then that will not work because you cannot encode structs to JSON. You can convert them to maps first but you’d still have a problem because you cannot encode the:__meta__field or any associations either, so you’d have to drop those keys from the map, e.g.This could be refactored into a helper function but even then it’s quite complicated and verbose. If you only need the
investment_nameyou could just map over that field and simplify it a little, but then you lose flexibility to access the rest of the record if you need toOverall, this is all going against the grain of the framework, and doing simple rendering work in the client that is much more easily done on the server in a clearer and more idiomatic way. I would suggest only using Alpine.js for interactive elements that server-rendering can’t perform, like drop-downs, hide/show elements, animation etc.
msimonborg
Just to clarify where the merger between HEEX and Alpine comes in, the outer curly braces in
x-data={ ... }are required in order to interpolate the Elixir code into your HTML.AGURRU
Thank you so much for your really in-depth answer. You are right it’s an ecto struct and your ways of doing this maybe could work but I think it’s over-complicating it. Your answer is helping me understand what is going on better which is invaluable. I’ve changed tack a little since my initial post and I think I can achieve what I want using just HEEX functions and CSS.
I’m not a frontend dev and I’m completely new to HTML/CSS/JS in terms of actually coding but needs must whilst we are hiring a new front end team member.
I am trying to have three (dynamic from a list) cards that act as radio inputs for a form. The tailwind playground I have copied below is a simplified version of what I am trying to achieve.
In terms of what this looks like using HEEX.
As you can see, I had to hardcode the radio input value, Label name and also the label field (this is to make sure the label-for value matches the radio input.)
I feel like I am part of the way there but in my head - I should be able to combine this with
and not hardcode so many parts of the form, especially the value and label.
I haven’t even begun to think about how I can add content to each card beyond just the label but that’s another bridge I need to cross.
cmo
What does the
investmentsdata look like?msimonborg
I’m glad it was helpful @AGURRU .
Yes I think the HEEX and CSS route is simpler and more maintainable. To simply translate the template code you have posted to a dynamic iteration of
@investmentsit would look something like this:A couple notes and assumptions about the line:
<.form let={f} for={:payment} action="/payments/new" >"/payments/new", you would likely replace this with the appropriate route helper e.g.Routes.payment_path(@conn, :new)or whatever is correct for your app:paymentinto the form fields e.g.radio_button(:payment, ...), you assign it to the form struct with<.form let={f} for={:payment} ... >and pass it around the form with thefvariable e.g.radio_button(f, ...).Paymentchangeset that you are working on, which would be assigned to a@changesetor maybe@paymentassign, in which case you should actually be doing this<.form let={f} for={@changeset} ... >AGURRU
This certainly has done the trick.
I kept my original <%= form for => code as it was working fine - I had tried having the @changeset before but because of the way I have set up my pages it’s not in the assigns for this particular page same goes for @payment.
I still feel like I am a little blind to the inner workings of what is going on at times - I had tried something nearly identical to this but was using an @ symbol in front of the variables I wanted to use. How do you know where to use interpolation vs @ vs an atom?
msimonborg
<.form ...></.form>is a newer function component that plays nicer with LiveView as I understand it, so I usually default to that since I also default to LiveViewTo hopefully clear up some of your confusion:
@for assigns that you pass into the template on thePlug.Conn, e.g.@investmentsassignsitself, which is available in the template without@@for regular variables that you declare inside the template, e.g.finform_forandinvestmentinfor investment <- @invesments do<%= ... %>must be used to generate raw output e.g.<p class="foo"><%= :foo %></p>. For tag attributes use curly braces e.g.<p class={:foo}>foo</p>. This works with both HTML tags and Phoenix function components. You can also do something like<p {assigns}>if you just want to pass an entire map or keyword list to the element/component as attributes.<%= form_for :payment ... %>you are just declaring that this is the"payment"form for the"payment"params, sending to your controller as%{"payment" => payment_params} = params. If you have aPaymentchangeset in your assigns then this name will be inferred for you when you write<%= form_for @changeset ... %>and you will still end up with%{"payment" => payment_params} = paramswith the added benefit of validations and error reporting