当前位置 : 主页 > 手机开发 > 其它 >

继承Phoenix / Elixir中的布局

来源:互联网 收集:自由互联 发布时间:2021-06-19
我需要为我的Phoenix / Elixir应用程序创建一个简单的布局层次结构,并从不同的布局继承我的模板.我知道如何为单个布局和多个模板做到这一点.但是我怎样才能从另一个布局继承一个布局
我需要为我的Phoenix / Elixir应用程序创建一个简单的布局层次结构,并从不同的布局继承我的模板.我知道如何为单个布局和多个模板做到这一点.但是我怎样才能从另一个布局继承一个布局呢?

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

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

更新:

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

在调用render时,可以通过在assign中传递布局键来嵌套布局:

<%= 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:

06001

对于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 %>
网友评论