Hi, i have macro:
defmacro from_db(var1) do
code = MyApp.get_code_from_db |> Code.eval_string
# the string code from db field is : "if 1 < 2 do IO.inspect var1 end"
unquote(code) #but doesn't work
How to make it works ? It means how to “unqoute” var1 which is inside string code taken from DB. ?
Thanks in advance .
Code.eval_string/3 accepts bindings as a second parameter. Also, you don’t need a macro here.
do: MyApp.get_code_from_db() |> Code.eval_string(var1: var1)
Further to the response from @mudasobwa, don’t forget that macros execute at compile time meaning that what you have crafted would read the string from the database during compilation. That may be exactly what you want to do, but it would be unusual.