In Phoenix
, is there some way that I can specify which View
to use instead of letting the Controller
inflect from the namespace?
I have multiple controllers and for each controller's view the methods are same. I would like to create a single view and use it with (almost) all of my controllers.
Is this possible? And more importantly, is this a bad practice or justified in my situation?
You can use put_view/2
to set the view module. It is not bad practice given your description. You can put the following code inside your controller:
plug :put_view, MyApp.TheView
Or you can modify the view for a single function inside your controller, e.g.:
def index(conn, _params) do
conn
|> put_view(MyApp.TheView)
|> render("index.html")
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With