But I get a compilation error like this:
unexpected operator ->. If you want to define multiple clauses, the first expression must use ->. Syntax error before: ‘->’
What am I doing wrong? I searched the documentation and the forum but could not find any solution that would compile.
The whole reason I’m using the template page_live.html.leex is so I don’t have 1000 lines of HTML inside my page_live.ex (with no coloring/syntax highlighting because the IDE considers it all “String”). Are you saying it’s better to cut the template down to a mere four lines of function calls and put everything else, front- and back-end, inside page_live.ex? What is the advantage of this? I’m still relatively new to Phoenix, so I’m sure there are things I’m overlooking.
OK well if I see anything like that, I will consider it. But I still need to figure out proper EEx case statement syntax. It seems like it would be documented somewhere.
Weird, we do exactly this a lot. And @kokolegorille the issue with content_tag is that it isn’t really usable with live view. You can’t for example do live_component inside of it and have it work correctly.
<%= case @event do %>
<% %{type: :research_status_changed, data: status_change} -> %>
stuff
<% %{type: :labeling, data: %{comment: comment, user: user}} = event -> %>
stuff
<% end %>
Hey, I run into similar problems when starting with liveview. For me it was mostly my due to non clean, highly convoluted code… from my messing programming. Disclaimer: I am not proficient in any ways. And your code doesn’t look convoluted. But we just see a part of it
Spinning up a fresh non-ecto liveview it all worked with your code.
But when doing something like this I can reproduce your error:
<%= case @nav do%>
<% nil %>
<% _ -> %>
unexpected operator ->. If you want to define multiple clauses, the first expression must use ->. Syntax error before: ‘->’
Yes, doing it this way results in an error “missing terminator: end for do”. It does not recognize the “end” when formatted this way.
I guess further analysis could maybe explain how this can be achieved too.
My approach is maybe to trivial to have a chance. But have you checked on which line the compiler hits the syntax error? And is there any “surrounding” code. Another case statement somewhere around?
This will not work the way you want it to, it will escape the inner HTML. <%= "<div></div>" %> shouldn’t be necessary, it should be possible to just <div></div>. I think @tschnibo is on to something, my guess is that @APB9785’s code is doing do%>
You know, I thought I had tried that already, but I guess not, because you’re totally right. The case statement must expect the first pattern to come immediately, and the comment beforehand was breaking syntax.
In the previous version, I was using nested if-else statements for this navigation switch, and the comments weren’t bothering anything. But now I see that in a case statement, the comments must come AFTER the pattern clause to match.
You can take each of those big groups of HTML, slap them in a “component” and then you insert something like a “fake html” tag into the code. The tag gets inflated into the contents of the component.
Now at this point you are correct in pointing out this is no different to using a partial. However, the syntax is similar to adding new HTML tags, so this also implies that the content of the tag gets passed in kind of like a param into the partial function.
I use it by creating a component say “BoilerPlate”, which is the outline of the form, it only contains components like “SideBar”, “TopNav” and “Content”. These inflate to their own components and so on. Content will be the wrapped content, which in your case will be one of several components, decided by the results of the case statement.
I’m then creating a folder with the same name as the component and further break that main content down into a small enough number of pieces that it’s understandable. I really like it!