在长生不老药中从视图调用渲染部分

call render partial from view in elixir

我正在和 Phoenix 一起学习 Elixir,只是陷入了一个非常愚蠢的问题。我想通过以下方式从我的索引模板中调用局部渲染:

#index.html.slim

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

为此,我创建了一个名为 shared_view.ex 的视图,如下所示:

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

我预计它会通过循环渲染 shared/_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.

但是没有渲染任何东西。而且我也没有收到错误。它只是渲染之前和之后的东西。

我不确定我在这里遗漏了什么。没有路由或控制器操作连接到“_game”部分,因为我认为这不是必需的(我已经习惯了 rails 并且它在那里工作)。

原来是拼写问题。有两个问题:

  1. 正如@radubogdan 所解释的那样,Slim 扩展不应是显式的。
  2. 如@Dogbert 所说,应使用 = for 而不是 - for 添加循环。

最后是这样的:

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.