I was looking to wrap a macro and read a lot of posts here and elsewhere but none seemed to really help me, as they often focussed on wrapping your own macro and I wanted to wrap an existing macro I had no access to, but couldn’t use their solutions as they often adjusted “the macro to be wrapped” or put the functionality in a function to call.
I found a solution myself using
Macro.expand_once and it works and is in prod and is nice, but I was wondering if anyone has a better idea or if there’s anything wrong with the solution? Partially, as I didn’t find it online easily I wanted to do a quick blog about it and for that I wanted to also check if I’m missing anything.
Off to the macro:
defmacro function_from_file(kind, name, file_name, arguments) do Macro.expand_once( quote do EEx.function_from_file( unquote(kind), unquote(name), unquote(This.Module.file_path(file_name)), unquote(arguments), engine: Phoenix.HTML.Engine ) end, __ENV__ ) end
as you can see, the intent is just wrapping
EEx.function_from_file/5 with pre defined options and easier path access.
Also, as I usually avoid macros until I can’t any more my macro knowledge isn’t super high