Since your SharedView module also has this quote block injected, it’s trying to import itself, which is causing the error. The easiest option is to my those function, i.e. my_func/1 to another module that you import in the view block instead of defining it on SharedView
SharedView is nothing special. It’s a view module like any other, but happens to be named Shared. You can for example use it like that in a controller: render(conn, SharedView, "partial.html", assigns) Likewise it does work in all other places where you can specify a view modules to render things.
If you want to share your posted function and not all the ability to render things just create a non-view helper module. Phoenix does that with the ErrorHelpers module.
SharedView is for shared templates. It’s not ideal to share functions in it with other views. To share function just copy what phoenix does with ErrorHelpers. There’s a ErrorHelpers module in the views folder and that module is imported in the web.ex file for views. ErrorHelpers is a plain elixir module, so you don’t have that circular import like with SharedView.
Another option is to go ahead and use SharedView but don’t insist on importing the function and instead alias SharedView. It means all the callsites will be SharedView.my_func() but it’s more explicit and clearer to people new to the codebase where that function is defined.
The more time I spend in Elixir the more I shift from imports to aliases.
A ‘View’ module (one that uses the web’s :view) compiles the templates in the same ‘name’ into the View module itself (specifically in ‘render/2’ functions).
So if you want something accessible from inside a template, then you make it accessible from inside a view module.
So say you have a view module named BlahView, and you have templates in a templates/blah directory, then anything you alias or import or just define straight in the BlahView will be accessible to the templates, just like if the templates where in BlahView too (because they actually are).
Usually, there is a module $MyApp.Web which has a function view/0, which returns a quoted block. Just add alias $MyApp.SharedView in the quote. You can then access that module from every view as SharedView, by adding alias $MyApp.SharedView, as: SV you can access it by using SV.