ryanditjia
My template consists of a lot of these:
<script src="<%= Routes.static_path(@conn, "/vendor/libs/autosize/dist/autosize.min.js") %>"></script>
<script src="<%= Routes.static_path(@conn, "/vendor/libs/chart.js/dist/Chart.min.js") %>"></script>
I’d like to be able to abstract it as follows:
view
defmodule MyAppWeb.LayoutView do
use MyAppWeb, :view
def prepend_vendor_path(path_to_file) do
Routes.static_path(@conn, "/vendor/" <> path_to_file)
end
end
template
<script src="<%= prepend_vendor_path("libs/autosize/dist/autosize.min.js") %>"></script>
<script src="<%= prepend_vendor_path("libs/chart.js/dist/Chart.min.js") %>"></script>
My question is how do I access @conn (or assigns) from the view module? It’s possible I don’t have the right mental model regarding views and templates yet. Currently I’m of the impression that views call templates, so whatever templates can access, views should be able to access that too.
Any help is appreciated. Thanks in advance.
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
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
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
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
If i need the conn inside the helper function, I pass it as a parameter…
But in case You really don’t have a connection, You still can pass the Endpoint instead.
LostKobrakai
Templates become functions on a view module, that’s correct. But like anywhere else the data passed to the function for rendering a certain template doesn’t magically make that data available to other functions on the same module.
ryanditjia
Thanks for your replies, I’m able to implement this by passing @conn to the helper function, which takes in conn and path. So view doesn’t have conn, only template does. This still confuses me, but I’ll make do for now.
LostKobrakai
A view module is a module like any other in elixir. It’s just holding some functions. Some of those functions body might be compiled based on external template files, but that doesn’t change anything for the fact that each function is called in isolation and that there isn’t any magic sharing of data between functions. It’s not a class holding some internal state. The example I had in my last post is almost literally what a view module looks like after the template file was compiled into it.
ryanditjia
Let me know if I’m correct here:
View’s
renderpassesassignsto template, the data inside theassignsmap can be accessed from template by prepending special syntax @ to the key. Hence,assigns.conn == @conn.–
Also another unimportant and possibly very confused question, but I’m seeking deeper understanding:
How do I replicate A to B here:
A (with template file)
B (rendering everything from view)