

Macros are also used for evading boilerplate when calling plugs, working with the routing table, etc.
#Liveview phoenix code#
Quick tip: if you use ElixirLS in VSCode, you can use the expand macro command (in the command palette) to look at the final code that will be generated with use. When you see something like use TodoWeb at the top of a module, it means that before compiling, Elixir will go to the module with that name, find a macro called _using_, and put the code that it generates inside the caller module. To pull other code into our modules (quickest import statements + boilerplate setup in the West).Macros are functions that generate code at compile-time. Let’s talk a bit about the magic incantations you might have seen in Phoenix code ( router is the biggest offender). As I said before: at each step, we are just moving the request further and further through composable functions. The endpoint, the router, and the controllers are plugs. In other words, Phoenix is itself a pipeline for transforming conn. They get compiled into the view module during the compilation process with macros. Views get most of their content from templates, which are HTML or JSON files with a dash of Elixir inside them. The controller launches a view that uses conn data to render the page for the user. Then conn gets forwarded to the router, which moves it through its pipeline (developers can define different ones to handle different requests) and passes it to its controller. It defines a common pipeline that all of the requests pass through. The endpoint is where all the requests land after being converted to conn by the server.
