Please show this module.
In general sigils are just a functions with a bit of syntactic sugar (~ notation). Functions and macros are not “magically enabled”. They need to be defined or imported within a module. The only “magic” is that functions and macros defined in Kernel and Kernel.SpecialForms are automatically imported to every module. Since verified routes are not even part of Elixir core we need to define or import them.
# definition
defmodule MyLib do
defmacro __using__(opts \\ []) do
quote do
def sigil_p(string, opts) do
# …
end
end
end
end
defmodule MyApp do
# use is short for:
# require MyLib
# MyLib.__using__(…)
use MyLib
end
# import
defmodule MyLib do
def sigil_p(string, opts) do
# …
end
end
defmodule MyApp do
import MyLib
# or use some macro with such import call
# require MyLib
# MyLib.macro_with_import_stuff(…)
# or same macro, but with name __using__ and use call as above
end
When generating phoenix app with:
$ mix archive.install hex phx_new --force
$ mix phx.new drone_shop
it should create a file called drone_shop/lib/drone_shop_web.ex with such code:
defmodule DroneShopWeb do
# …
# this calls html_helpers private function …
def live_view do
quote do
use Phoenix.LiveView,
layout: {DroneShopWeb.Layouts, :app}
unquote(html_helpers())
end
end
# …
# this function have some shared code for many parts of your application
# at the bottom you should another call
# this time to verified_routes private function
defp html_helpers do
quote do
# …
# Routes generation with the ~p sigil
unquote(verified_routes())
end
end
# we are finally here!
# we are using some module
# this is the second case I mentioned i.e. import within a macro
def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: DroneShopWeb.Endpoint,
router: DroneShopWeb.Router,
statics: DroneShopWeb.static_paths()
end
end
@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
# This is called first! It then call a live_view function …
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
end
You can find an import call in phoenix source:
https://github.com/phoenixframework/phoenix/blob/7b5cd358aadb507cd90aa3a52e013f9e9e947ac4/lib/phoenix/verified_routes.ex#L121-131
And the definition of said sigil is here:
https://github.com/phoenixframework/phoenix/blob/7b5cd358aadb507cd90aa3a52e013f9e9e947ac4/lib/phoenix/verified_routes.ex#L202-L210






















