Reputation: 13
By default one can’t use subfolders to render partial templates in Elixir Phoenix framework but it can be changed in the web_app module view definition. That is one of the changes I always make when starting a new Phoenix project.
def view do
quote do
use Phoenix.View,
root: "lib/demo_web/templates",
namespace: DemoWeb,
# for template subfolders usage
pattern: "**/*"
.....
end
end
So I was wondering that if it is not possible by default, there may be a good reason.
Someone told me this but I'm not understanding:
If you nest templates in a outer folder, which already has a view module setup the inner templates will be compiled into the outer view module as well, which can be problematic.
Please can you explain me how it can be problematic?
Upvotes: 1
Views: 200
Reputation: 12700
By allowing subfolders for templates, you are basically saying to phoenix that each subfolder should be compiled into the corresponding top-level view.
This may not be a problem for a small project, where you don't have many pages, but imagine you start having something more complex where you decide to nest views?
I don't even know how Phoenix would react in this case, because basically that would mean your template will be compiled in two different views...
If it doesn't work, you might spend time figuring out why, and if it does, you might at one point use the same method name in the top-level and sub-folder views, in which case it will no longer be clear which one you are referring to.
Have a look at the templates guide, the last section is called Shared Templates Across Views and it says:
Often, we find that small pieces of data need to be rendered the same way in different parts of the application. It's a good practice to move these templates into their own shared directory to indicate that they ought to be available anywhere in the app.
This should provide you with most of what you need without changing the default Phoenix behavior
Upvotes: 0