AlchemistCamp

AlchemistCamp

How would you refactor this? (shared templates helper)

I’ve just finished writing up how I share templates between Phoenix views, and it turns out the easiest and clearest way I’ve found to do it explicitly goes against the suggestion NOT to inline any functions in the my_app_web.ex file.

Basically, I’ve added this function to the view quote do part:

def render_shared(template, assigns \\ []) do
  render(MyAppWeb.SharedView, template, assigns)
end

The reason I did it because SharedView is a view and if I put the function there and import the function into my_app_web.ex, the following compile error occurs:

== Compilation error in file lib/my_app_web/views/shared_view.ex ==
** (CompileError) lib/my_app_web/views/shared_view.ex:2: you are trying to use the module MyAppWeb.SharedView which is currently being defined.

This may happen if you accidentally override the module you want to use. For example:

    defmodule MyApp do
      defmodule Supervisor do
        use Supervisor
      end
    end

In the example above, the new Supervisor conflicts with Elixir's Supervisor. This may be fixed by using the fully qualified name in the definition:

    defmodule MyApp.Supervisor do
      use Supervisor
    end

    expanding macro: MyAppWeb.__using__/1
    lib/my_app_web/views/shared_view.ex:2: MyAppWeb.SharedView (module)
    (elixir) expanding macro: Kernel.use/2
    lib/my_app_web/views/shared_view.ex:2: MyAppWeb.SharedView (module)
    (elixir) lib/kernel/parallel_compiler.ex:229: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

The question is: how big of a deal is it really, to have an inline function as I currently do? And where would be a logical place to refactor the function to?

Any suggestions / feedback much appreciated!

First 7 of 7 Posts! Switch mode

gregvaughn

gregvaughn

Firstly, I wouldn’t sweat a one-liner in my_app_web.ex. Things get bad when over time that grows to a dozen or two, which I think is the real caution behind the advice you heard to never do it.

However, we might could even simplify that too. What if you alias MyAppWeb.SharedView in there? Then every other view can call SharedView.render(template, assigns)

AlchemistCamp

AlchemistCamp

That’s a good idea! It’s 19 keystrokes vs 13 to use it each time, though:

  • SharedView.render
  • render_shared
LostKobrakai

LostKobrakai

The big drawback with import is that it does cause a compile time dependency. Each time you’ll update SharedView it’ll cause recompilation on all of your view modules. Aliases on the other hand don’t do that. This was the reason for phoenix to switch from importing path helpers to aliasing the module.

gregvaughn

gregvaughn

Well if you want to :golf: then
alias MyAppWeb.SharedView, as: SV
and call with SV.render
but I wouldn’t recommend it. Code is read much more often than it is written.

AlchemistCamp

AlchemistCamp

Don’t want to golf and SV.render definitely isn’t as clear or searchable as either render_shared or SharedView.render. I do have a history of severe RSI, and even before that valued succinctness, though!

FWIW, I think render_shared is the least awkward name, at least how I think of what’s being done. I like the alias idea, just may need to change multiple names.

I’ll probably just stick with the current approach based on your feedback that an inline one-liner isn’t a huge problem, unless there’s a cleaner/simpler way.

AlchemistCamp

AlchemistCamp

It’s hard to imagine doing that often… the only view modules I tend to alter are those with embedded eex for meta tags and other SEO-related concerns.

AlchemistCamp

AlchemistCamp

Okay… with just one suggestion and none that maintain the same interface (which was the goal of the entire endeavor), I’ll just “accept” the current structure.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement