Conn or Endpoint in path helpers?

What is the difference between page_path(conn, :index) and page_path(Endpoint, :index)? Are there any downsides to using Endpoint instead of conn? I ask because it’s tiresome to pass conn as an assign when rendering templates that use path helpers (<%= render "form.html.eex", conn: @conn %>).

1 Like

The conn carries the endpoint it came from, so in either case we look up the url/host/port details for the link to build from the endpoint. The main difference is passing the Endpoint module is fixed to config, while passing the conn will take things like script_name and forwarded paths into consideration when building the links. If you have the conn, you should pass that, but the Endpoint is useful for cases where you want to render templates with and without the context of a connection, such as emails.

5 Likes

With regards to being “tiresome” passing arguments – this is one of the tenants of functional programming. We explicitly pass data to functions. You can also pass all assigns to render, such as render MyView, "my_template.html", assigns

3 Likes

Thanks!

I was not aware of assigns—I always used @conn.assigns.