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.
If i need the conn inside the helper function, I pass it as a parameter…
def prepend_vendor_path(conn, path_to_file) do
Routes.static_path(conn, "/vendor/" <> path_to_file)
end
But in case You really don’t have a connection, You still can pass the Endpoint instead.
def prepend_vendor_path(path_to_file) do
Routes.static_path(MyAppWeb.Endpoint, "/vendor/" <> path_to_file)
end
4 Likes
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.
defmodule MyAppWeb.LayoutView do
use MyAppWeb, :view
def render("mytemplate.html", assigns) do
# render html with assigns, which includes the conn
end
def prepend_vendor_path(path_to_file) do
# Where would the conn come from?
Routes.static_path(conn, "/vendor/" <> path_to_file)
end
end
3 Likes
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.
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.
Let me know if I’m correct here:
View’s render
passes assigns
to template, the data inside the assigns
map 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)
# layout_view.ex
defmodule MyAppWeb.LayoutView do
use MyAppWeb, :view
end
# app.html.eex
<main><%= render @view_module, @view_template, assigns %></main>
B (rendering everything from view)
# layout_view.ex
defmodule MyAppWeb.LayoutView do
use MyAppWeb, :view
# this is guesswork that doesn’t work
def render("app.html", assigns) do
"<main><%= render @view_module, @view_template, assigns %></main>"
end
end