How to write elixir code in html(Phoenix)

Hello, Every sample shows me like this:

<p><%= handler_info @conn %></p>

but I need this:

<p class="<%= handler_info @conn %>">test</p>

when I want to use it , I have an error. if I use Tag or raw, my html template is confused.

how can I use like that?

:wave:

What’s the final HTML that you want?

<p class="???">test</p>

What does handler_info(@conn) return?

for example:

name_of_class = Db.classname

<p class="<%= name_of_class%>">test</p>

after compile:

<p class="drag test cg">test</p>

I have written handler_info(@conn) for example not real code. would you mind seeing the above code again, Please?

name_of_class = Db.classname()

<p class="<%= name_of_class %>">test</p>

would work fine, I think, if Db.classname() returns "drag test cg". But it wouldn’t be replaced during compilation, though. For that you’d need to either use macros or hard code the value. Since it’s a function call, it would be replaced every time the template is rendered.

1 Like

Hmm,

Please see the code :

<%= Enum.sort(menu) |> Enum.each( %>
  <div class="dropdown-menu" aria-labelledby="navbarDropdown">
    DEV
  </div>
<% ) %>

I want to use Enum.each instead of for loop like this , but I have an error :

for loop works me but enum.each not :

Try using Enum.map instead. Enum.each doesn’t have any output, so that’s why it doesn’t work. for comprehensions and Enum.map would work similarly in your case.

2 Likes

in Enum.map I also have this problem, I think because ‘)’

like :

 <%= Enum.map(menu, fn({_key, {name, addres, sub}}) -> %>
        <div class="basket">
        <div class="container">
 <%= end) |> Enum.sort %>
1 Like

in Enum.map I also have this problem

What problem?


Try this

<%= Enum.map(menu, fn {_key, {name, address, sub}} -> %>
  <div class="basket"></div>
  <div class="container"></div>
<% end) %>

Does it work?

Note that there is no = in <% end) %>.

3 Likes

errors:

== Compilation error in file lib/trangell_html_site_web/views/layout_view.ex ==
** (SyntaxError) lib/trangell_html_site_web/templates/layout/_navigation.html.eex:67: unexpected token: )
    (eex) lib/eex/compiler.ex:45: EEx.Compiler.generate_buffer/4
    (eex) lib/eex/compiler.ex:54: EEx.Compiler.generate_buffer/4
    (phoenix) lib/phoenix/template.ex:378: Phoenix.Template.compile/2
    (phoenix) lib/phoenix/template.ex:186: anonymous fn/3 in Phoenix.Template."MACRO-__before_compile__"/2
    (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
    (phoenix) expanding macro: Phoenix.Template.__before_compile__/1
    lib/trangell_html_site_web/views/layout_view.ex:1: TrangellHtmlSiteWeb.LayoutView (module)
    (elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
```

your code works!!

but how can I sort? by they position ?

my map :

def main_menu do
    menu = %{
      a_home: {"خدمات", "/", [
          {"ops1", "/sup1"}, {"test1", "/sup2"}, {"test2", "/sup3"}
        ]},
      b_blog: {"بلاگ", "/blog", [
          {"ops2", "/sup1"}, {"test3", "/sup2"}, {"test4", "/sup3"}
        ]},
      c_about: {"درباره ما", "/about", [
        ]},
      d_contact: {"ارتباط با ما", "/contact", [

        ]}
    }

I need it to load like this :

a_homed_contact but if I remove a form a_home , it will load

b_blog
c_about
d_contact
home

but home should be loaded at first.

If you need order, maybe try storing it in a list:

def main_menu do
    menu = [
      home: {"خدمات", "/", [
          {"ops1", "/sup1"}, {"test1", "/sup2"}, {"test2", "/sup3"}
        ]},
      blog: {"بلاگ", "/blog", [
          {"ops2", "/sup1"}, {"test3", "/sup2"}, {"test4", "/sup3"}
        ]},
      about: {"درباره ما", "/about", [
        ]},
      contact: {"ارتباط با ما", "/contact", [

        ]}
    ]
2 Likes