【问题标题】:Using Phoenix Elixir render assigns to include partials使用 Phoenix Elixir 渲染分配以包含部分
【发布时间】:2016-07-24 01:09:14
【问题描述】:

在我的 app.html.eex 文件中,我有:

<%= assigns[:feet]%>

在我的控制器中,我有: def index(conn, _params) do render(conn, "index.html", feet: "feet.html") end 我想这样做,以便可以在不同的子视图中导入不同的脚。例如:

def :edit(conn, _params) do render(conn, "edit.html", feet: "edit-feet.html") end 不幸的是,它只是打印“feet.html”而不是导入 feet.html。

这有可能吗?

【问题讨论】:

    标签: elixir phoenix-framework


    【解决方案1】:

    您需要拨打Phoenix.View.render/3:

    <%= render YourView, @feet, [] %>
    

    如果设置feet 是可选的,并且您不想渲染任何不存在的东西,您可以这样做:

    <%= if feet = assigns[:feet] do %>
      <%= render YourView, feet, [] %>
    <% end %>
    

    YourView 替换为包含feet.htmledit-feet.html 的视图。

    如果您需要将任何assigns 传递给这些模板,您可以将[] 替换为您要传递的值:

    <%= render YourView, @feet, foo: "bar", baz: "quux" %>
    

    【讨论】:

    • ` ` 正是我想要的。
    • @mchavezi Pedantic 评论:如果您使用的是@feet,则无需使用feet =。或者你应该在if 中使用feet。您评论中的版本有一个未使用的变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    相关资源
    最近更新 更多