augnustin
Unquote with parameter in block
So I have this macro:
defmacro content(_opts \\ [], do: block) do
quote location: :keep do
def page_view(var!(conn)) do
import Kernel, except: [div: 2, to_string: 1]
import ExAdmin.ViewHelpers
use Xain
_ = var!(conn)
markup safe: true do
unquote(block)
end
end
end
end
Which is used like that:
content do
# Whatever code
end
How can I pass the conn variable to the content block?
content do
IO.inspect(conn)
end
Can’t make it work. ![]()
Thanks
Marked As Solved
LostKobrakai
defmodule TestXain do
defmacro content(do: block) do
block =
quote do
_ = var!(conn)
import Kernel, except: [div: 2, to_string: 1]
use Xain
markup safe: true do
unquote(block)
end
end
quote location: :keep do
def page_view(var!(conn)), do: unquote(block)
end
end
end
defmodule TestXain.View do
import TestXain
content do
IO.inspect(conn)
end
end
iex(1)> TestXain.View.page_view(1)
1
{:safe, "1"}
This seems to work fine for me and circumvents the whole problem of unquote fragments.
3
Also Liked
LostKobrakai
See my last paragraph here: I don't understand quote/unquote: why do we need them? - #4 by LostKobrakai
More on unquote fragments: Kernel.SpecialForms — Elixir v1.20.2
5
LostKobrakai
You could build the macro to be called like that:
content(conn) do
IO.inspect(conn)
end
or
content conn do
IO.inspect(conn)
end
I’d not jump to unhygienic variables without a good reason.
2
OvermindDL1
I personally prefer something like:
content do
conn -> IO.inspect(conn)
end
You can either decompose it to grab the head as the var, or just use it as a case context with full matching and all, which is also so easy to do in quotes. ![]()
2
Popular in Questions
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
I am trying to implement my new.html.eex file to create new posts on my website.
new.html.eex:
<h1>Create Post</h1>
<%= ...
New
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
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
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 t...
New
Other popular topics
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
What learn first? Rust or Elixir
Hi Elixir community!
I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
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
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
I would like to know what is the best IDE for elixir development?
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








