How to share partials between layouts in Phoenix?

I have two layouts:

  1. app.html.eex
  2. dashboard.html.eex

dashboard.html.eex should be rendered in app.html.eex. Some templates will be rendered in dashboard.html.eex layout, some will be rendered in app.html.eex layout directly.

I checked http://stackoverflow.com/questions/39523427/inheriting-layouts-in-phoenix-elixir and https://www.reddit.com/r/elixir/comments/5366dc/how_can_i_render_a_child_layout_inside_a_parent/. And got this in app.html.eex:

<%= render @view_module, @view_template, Map.put(assigns, :layout, {App.LayoutView, "dashboard.html"}) %>

and got this in dashboard.html.eex:

<%= render @view_module, @view_template, assigns %>

Now every templates will be rendered into dashboard.html.eex.

So I have to setup two independent layouts app.html.eex and dashboard.html.eex, since there are many common parts, I would split them into partials and compose the partials in layout file. The problem here is the code would be hard to follow.

I’m wondering if there’s any better way to handle this.

1 Like

I think it would be much clear to share the codes in templates instead of layout.

1 Like