gpt4 book ai didi

elixir - 继承 Phoenix/Elixir 中的布局

转载 作者:行者123 更新时间:2023-12-03 16:42:15 24 4
gpt4 key购买 nike

我需要为我的 Phoenix/Elixir 应用程序创建一个简单的布局层次结构,并从不同的布局继承我的模板。我知道如何为单个布局和多个模板做到这一点。但是我究竟如何才能从另一种布局继承一种布局?

比如说,层次结构是 layout1 -> layout2 -> layout3 和 template2(layout2), template3(layout3)。

在文档中没有提到这一点。

更新 :

基本布局就是基本布局——类似于 OOP 中的基类,它不知道它的子布局——有多少 ,如果有的话。因此,从基础调用“render children1”是没有意义的。

最佳答案

布局可以通过传递 layout 来嵌套键入 assigns调用渲染时:

<%= render @view_module, @view_template, Map.put(assigns, :layout, {MyApp.LayoutView, "nested.html"}) %>

这是 Phoenix.View.render/3 文档中的相关部分

Assigns

Assigns are meant to be user data that will be available in templates. However there are keys under assigns that are specially handled by Phoenix, they are:

  • :layout - tells Phoenix to wrap the rendered result in the given layout. See next section.

The following assigns are reserved, and cannot be set directly:

  • @view_module - The view module being rendered
  • @view_template - The @view_module's template being rendered ## Layouts Templates can be rendered within other templates using the :layout option. :layout accepts a tuple of the form
    {LayoutModule, "template.extension"}.

To render the template within the layout, simply call render/3 using the @view_module and @view_template assign:

  <%= render @view_module, @view_template, assigns %>


对于 3 种布局,您可以执行以下操作:
# Controller
render(conn, "index.html", nested_1: "nested_1.html", nested_2: "nested_2.html")

# app.html.eex
<%= render @view_module, @view_template, Map.put(assigns, :layout, {MyApp.LayoutView, assigns.nested_1}) %>

# nested_1.html.eex
<%= render @view_module, @view_template, Map.put(assigns, :layout, {MyApp.LayoutView, assigns.nested_2}) %>

# nested_2.html.eex
<%= render @view_module, @view_template, assigns %>

关于elixir - 继承 Phoenix/Elixir 中的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39523427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com