ryanditjia

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.

Showing Posts 1 to 5

kokolegorille

kokolegorille

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
LostKobrakai

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.

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
ryanditjia

ryanditjia OP

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

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

ryanditjia OP

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
— 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
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
asweet-confluent
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 Top

JesseHerrick
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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews