Embedding nested templates with similar names

Imagine you have this existing directory

    │   admin.html.heex
    │   employee.html.heex   
    │   admin.html.heex
    │   employee.html.heex

In the previous version using Phoenix.View, we can simply render templates with similar names by differentiating them using path for example:
render UserView, "admin/admin.html.heex"
render UserView, "customer/admin.html.heex"

But now when we migrate them to Phoenix.Component, we will now rely on using embed templates like so:
embed_templates "users/**/*"

Embed templates convert templates to functional components using their filename as the function name. But since the path will be disregarded when embedding templates we will now have two admin components and two employee components. The compiler will warn us with a message similar to this:
warning: this clause for admin/1 cannot match because a previous clause at line 2 always matches lib/my_web/views/users/admin.html.heex:2

How do we handle this case? Does Phoenix intentionally design it like this so we will be forced to write a specific module to handle sub-templates for clearer context? For example, instead of UserView, we should now have User.AdminView and User.CustomerView?

Multiple modules is the way to go for overlapping names. But you can also continue to use pheonix_view if you like.

1 Like

I see. Splitting views into sub-views sounds good. Phoenix.View, however, uses the render/2 function which as far as I know does not have efficient change tracking (changing one of its assign will re-render the whole template). I just thought there was a trick in using embed_templates for cases like this :slight_smile: