tom-gerken

tom-gerken

Phoenix dynamically generate radio buttons

I’m trying to generate some radio buttons dynamically. It’s easy enough when I have two options I can use = radio_button(f, :field_name, “false”) and radio_button(f, :field_name, “true”) . But what if I want to generate several options from a map? Are there any markup generators I can use to keep my code dry? Like: TemplateHelper.generate_radio(data)?

Marked As Solved

mikemccall

mikemccall

You can use the render function in a view. You can create a shared view (name it whatever, SharedView, FormHelpersView, etc) and move the rendering there.

# in the template, form.html.eex
render MyApp.SharedView, "radio_group.html", form: f, data: @data, field: :field %>

2 options for the shared view template. Return the html from the view’s render function or create a template.

defmodule MyAppWeb.SharedView do
  use MyAppWeb, :view
  
  def parse_function(data) do 
    ...
  end

  # return html from view function
  def render("radio_group.html", %{form: f, data: data, field: field) do
    ~E(
         <div class="form-control">
           <%= for l <- parse_function data do %>
           <div class="pretty p-default p-curve">
             <%= radio_button(f, field, elem(l, 1)) %>
              <div class="state">
                <label><%= elem(l, 0)%></label>
             </div>
           </div>
          <% end %>
      </div>
    )
  end

end

If you do not want to return the html in the view function you can create a template for it like any other view template/shared/radio_group.html.eex.

Reference:
sigil_E/2

Phoenix View

The examples shown for rendering custom scripts in the phoenix docs for render_existing/3 really helped me.

Also Liked

peerreynders

peerreynders

What about using a list comprehension?

https://hexdocs.pm/phoenix/templates.html#examples

scan down to Displaying Lists

tom-gerken

tom-gerken

Mike thank you for the response, and the links to read more information. I really appreciate how this community values helping and learning! In that spirit, I just wanted to add one piece I learned is that the close parenthesis “)” symbol needs to be escaped in sigil E. So I had to rewrite

<%= radio_button(f, field, elem(l, 1)) %>

as

<%= radio_button(f, field, elem(l, 1\)\) %>

NobbZ

NobbZ

Any closing delimiter matching the opening delimiter has to be escaped, that is by design of the sigils.,

As a rule of thumb, when you have multiline content just use """ or '''. They will rarely occur in your literal. If your snippet were a onliner, you could also use ', [/], or {/} as delimiter, which seem to not clash on a first glance.

Last Post!

NobbZ

NobbZ

Any closing delimiter matching the opening delimiter has to be escaped, that is by design of the sigils.,

As a rule of thumb, when you have multiline content just use """ or '''. They will rarely occur in your literal. If your snippet were a onliner, you could also use ', [/], or {/} as delimiter, which seem to not clash on a first glance.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement