sodapopcan
I’m having a bit of trouble wrapping my brain around how I would pass a dynamic fragment to ~p inside a macro:
defmacro __using__(opts) do
@path opts.path
quote do
def handle_event("event", _, socket) do
path = ~p"/#{@path}"
# ...
{:noreply, socket}
end
end
end
I’ve tried a fair number of functions from the Macro module both inside, namely escape and expand/expand_once which are the ones I though would do it as well as unquote (and even quote for good measure). I understand that passing an argument to the ~p macro is going to quote it and when it’s called already inside a quote block, well, uh… oh boy
As I have it does produce the correct route but of course gives a console warning. I feel like this question must have been answered before but I can’t find it and my brain hurts. At the very least I’m treating this forum as a rubber duck but any help would be much appreciated.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
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
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 5 of 5 Posts
0xG
There is some macro confusion here.
This changed version compiles:
But there is one more problem.
Since you are using dot syntax, I assume you’re trying to pass a map. Instead use a keyword. IIRC, keywords are not escaped:
sodapopcan
Hey hey, thanks for the response and welcome to the forum!
I’ve tried your suggestions and I’m still getting the warning. Are you seeing it? Maybe I messed up still. My problem was never that it didn’t compile—it compiled and produced the correct path, but it failed to be verified. The warning is
no route path for MyAppWeb.Router matches "/#{"some-path"}"so I need to be able to pass the~pmacro and already expanded variable… which is what I’m having a hard time wrapping my head around.LostKobrakai
That’s likely the clue here. The compile time validation doesn’t see
~p"/some-path", but~p"/#{"some-path"}". Evaluate both and it’ll be the same, but their AST/the code isn’t. The latter is not using a static string, even if the dynamic input takes a static value.You likely want this:
path = sigil_p(unquote("/#{path}"))– build the static path before unquoting the result into the sigil macro call.0xG
Hmm, I think
sigil_p/2pattern matches against ast and unquoting that expression results in fully evaluated binary.I bet there would be a more intuitive solution but this works:
So I passed the simplified version of desired ast:
sodapopcan
Thanks for your help, @0xG and @LostKobrakai. I’m ultimately going with a slightly more verbose but more flexible (and less brain-bendy for me) solution. I though I was only going to be dealing with URLs in the form of:
~p/resource/#{resource}but it turns out that is not the case and don’t want to get too crazy! Thanks again!