从elixir

时间:2016-12-02 18:47:40

标签: rendering elixir phoenix-framework

我正在和菲尼克斯学习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 个答案:

答案 0 :(得分:4)

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

  1. 细长的延伸不应该是明确的,正如@radubogdan解释的那样。
  2. 循环应该使用= for而不是- for添加,正如@Dogbert所说。
  3. 最后它看起来像这样:

    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.
    
相关问题