ntonnelier
ntonnelier

Reputation: 1549

call render partial from view in elixir

I'm learning Elixir with Phoenix and just got stuck in a pretty dumb point. I want to call the render of a partial from inside my index template the following way:

#index.html.slim

- for element_here  <- array_here do
  = render MyApp.SharedView, "_game.html.slim", element: element_here

For this I created a View called shared_view.ex that looks like this:

defmodule MyApp.SharedView do
  use MyApp.Web, :view
  def render("game", _assigns), do: "/shared/_game.html.slim"
end

I expected it go through the loop rendering shared/_game.html.slim, which I copy here:

.col-md-4.portfolio-item
  a href="#"
    img.img-responsive alt="" src="http://placehold.it/700x400" /
  h3
    a href="#"  Project Name
  p Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.

But nothing is being rendered. And I'm not getting an error neither. It just render the stuffs before and after that.

I'm not sure what I'm missing here. There's no route or controller action connected to "_game" partial because I didn't think it was neccesary (I'm used to rails and it works this way there).

Upvotes: 0

Views: 1192

Answers (1)

ntonnelier
ntonnelier

Reputation: 1549

Turned out to be a spelling issue. There were two problems:

  1. Slim extension should not be explicit, as @radubogdan explained.
  2. Loop should be added using = for instead of - for, as @Dogbert said.

In the end it looks like this:

index.html.slim

= for element_here  <- array_here do
  = render MyApp.SharedView, "_game.html", element: element_here

shared_view.ex

defmodule MyApp.SharedView do
  use MyApp.Web, :view
  def render("game", _assigns), do: "/shared/_game.html.slim"
end

_game.html.slim

.col-md-4.portfolio-item
  a href="#"
    img.img-responsive alt="" src="http://placehold.it/700x400" /
  h3
    a href="#"  Project Name
   p Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.

Upvotes: 5

Related Questions