augnustin
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
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #security
- #elixirconf-eu
- #api
- #forms
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
LostKobrakai
This seems to work fine for me and circumvents the whole problem of unquote fragments.
augnustin
Thanks a lot for your answers.
This works like a charm. It even works with the original version, which I did not think of testing since I could not imagine it would.
How does the block knows about
conn? For a almost functional language that is elixir, this is amazing to see a named variable coming in like this!But it does solve my issue, which is great!
LostKobrakai
Using
var!makes the variable unhygienic, so it bleeds into the inner blocks scope. If you consider the resulting AST you’ll notice, that the inner block just refers to a variable like this{:conn, [], Some.Module}, where the last part is the context of the variable.var!makes it so that the contexts match, while without that they won’t.The resulting AST doesn’t really care if it came out of a macro or not. With it it looks like you wrote:
Which is obviously fine code.
augnustin
Yes, I imagined this was equivalent to:
The only thing that makes me slightly uncomfortable is that the variable is named so one needs to read the calling code (or the doc) to know what variables are available but I guess that’s how things go!
Thanks for your support
LostKobrakai
You could build the macro to be called like that:
or
I’d not jump to unhygienic variables without a good reason.
OvermindDL1
I personally prefer something like:
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.
augnustin
Those two solutions seem much cleaner in terms of readability.
That said, I’m going for the undeclared version since it is the original from a lib (ex_admin) and I don’t need to change it here.
silviurosu
I like very much your idea. Can you give an example how can I write a macro like this?
I would like to be able to call it like this:
I can not figure out how to write the macro. What I have by now is:
OvermindDL1
You’d want to instead do this function as something like this instead: