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!
Trending in Questions
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
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
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
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
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
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
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
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
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
@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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 7 of 7 Posts!
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.SharedViewin there? Then every other view can callSharedView.render(template, assigns)AlchemistCamp
That’s a good idea! It’s 19 keystrokes vs 13 to use it each time, though:
SharedView.renderrender_sharedLostKobrakai
The big drawback with
importis that it does cause a compile time dependency. Each time you’ll updateSharedViewit’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
Well if you want to
then
alias MyAppWeb.SharedView, as: SVand call with
SV.renderbut I wouldn’t recommend it. Code is read much more often than it is written.
AlchemistCamp
Don’t want to golf and
SV.renderdefinitely isn’t as clear or searchable as eitherrender_sharedorSharedView.render. I do have a history of severe RSI, and even before that valued succinctness, though!FWIW, I think
render_sharedis 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
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
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.