I wonder if i can help, but first: It sounds like you’re using Phoenix, but the code doesn’t look like you’re using Phoenix. When i create a new phoenix project my controllers need to include
use <namespace>Web, :controller
so that the Phoenix.Controller.render/3
function becomes available.
(That means my simple controller looks like this:
defmodule T21517Web.HomeController do
use T21517Web, :controller
def index(conn, _params) do
render(conn, "index.html")
end
end
, where the namespace for the modules would of course differ in your case.)
The same way your view module should have a
use <namespace>Web, :view
Then i added the function clause you seem to want to include:
def render("_test1.html", %{data: data}) do
# I'm just returning some text here...
"My data: #{inspect(data)}"
end
so my whole view module looks like this:
defmodule T21517Web.HomeView do
use T21517Web, :view
def render("_test1.html", %{data: data}) do
"My data: #{inspect(data)}"
end
end
And now i can call this new <namespace>Web.HomeView.render/2
clause from within my template:
<h1>Home</h1>
<%= render("_test1.html", data: [1, 2, 3]) %>
I did not include the view module as a parameter to render/2
, because as @OvermindDL1 pointed out the template is “in the context of the view module”, so you can just call the render method directly.
If i missunderstood what you’re trying to do i can also imagine you wanting to call another view from within your home view. In my test i created a view module T21517Web.BlaView
:
defmodule T21517Web.BlaView do
use T21517Web, :view
def render("bla.html", %{data: data}) do
"This is Bla's data: #{inspect(data)}"
end
end
and in my template for the home view i added a call to the render function from that new module:
<h1>Home</h1>
<%= render("_test1.html", data: [1, 2, 3]) %>
<%= render(T21517Web.BlaView, "bla.html", data: [4, 5, 7]) %>
Everything seems to work fine for me: