AlchemistCamp

AlchemistCamp

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!

Showing Posts 1 to 7

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 OP

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 OP

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 OP

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 OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews