Best way to build a template-wide render helper in Phoenix?

I’m in the middle of building a simple render helper called renderp, which expands this:

= renderp [:dashboard, :shared, "partial"], model: @model


= render MyAppWeb.Dashboard.SharedView, "partial.html", model: @model

I plan to extend this later with other useful features to clean up my views (open to suggestions for hex packages which already do this!).

I did originally chuck my renderp function in my layout_view, but now need access to it across all of my views. I tried to add it to the view modules under my myapp_web.ex file:

def view do
  quote do
    import etc
    import MyAppWeb.LayoutHelpers

defmodule MyAppWeb.LayoutHelpers do
  def renderp, do: render

But then I’m not sure what I need to do inside of layout helpers to give me access to the original Phoenix render function. I can’t use MyAppWeb, :view because that creates a conflict due to circular dependencies, and yet I also can’t seem to simply use Phoenix.View, root: etc etc << don’t like anyway, would prefer that config to stay in one place.

Anyone shed light on how I might cleanly go about this? And I know explicitness is preferred in most regards - this isn’t an argument for/against.

Have gone ahead and drawn inspiration from:

Can you explicitly call Phoenix.View.render/3 from your helper function?


Yep, that’s what I ended up doing! Thanks.

1 Like