augnustin

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. :frowning:

Thanks

Marked As Solved

LostKobrakai

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.

Also Liked

LostKobrakai

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.

OvermindDL1

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. :slight_smile:

Where Next?

Popular in Questions Top

_russellb
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
vertexbuffer
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
9mm
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
qwerescape
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
shahryarjb
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
JulienCorb
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
pmjoe
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
RisingFromAshes
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
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 t...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Darmani72
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
electic
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
AngeloChecked
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
aesmail
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
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 52341 488
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
Brian
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement