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!
Trending in Questions
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
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
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
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
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.