jakub-zawislak

jakub-zawislak

EEx - multiple anonymous function as arguments

I want to write:

<%= my_function f, fn arg -> %>
  some html
<%= end, fn arg -> %>
  another html
<% end %>

But I got a compilation error in third line: unexpected token: end

This code doesn’t produce error:

<%= my_function f, fn arg -> nil end, fn arg -> %>
  another html
<% end %>

Am I doing something wrong?

Most Liked

michalmuskala

michalmuskala

I think the second <%= should be a <%, but I haven’t tried it out.

OvermindDL1

OvermindDL1

It is not just anonymous functions, eex does not like non-blocks (like do/end) being broken up between different eex statements, it expects everything to be in a single statement as far as my testing has shown me.

At those times I just break up the template into smaller templates and just pass in the anonymous function calls to them, plus it makes the templates smaller and ‘usually’ more readable. :slight_smile:

I’ve not tried this, just thought of it, but you could try:

<%= my_function f, fn arg -> ~e"""
   some html
""" end, fn arg -> ~e"""
  another html
""" end %>

So just embed eex templates in the eex templates (which will not have a run-time overhead at all, eex templates, no matter how deep, inline into an iolist). ^.^

jakub-zawislak

jakub-zawislak

Now it works. Anyway, putting this functions in variables is more readable

jakub-zawislak

jakub-zawislak

Still doesn’t work. I think that EEx just doesn’t like when there is something after anonymous function with html. This also doesn’t work:

<%= my_function f, fn arg -> %>
  some html
<%= end, 2137 %>
jakub-zawislak

jakub-zawislak

missing terminator: end (for "fn" starting at line 3)
:smiley:

So this is the only solution for now

<% template1 = fn arg -> ... end %>
<% template2 = fn arg -> ... end %>
<%= my_function f, template1, template2 %>

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
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

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
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 49522 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement