Nao000のぶろぐ

蝶を追っている少年になりたい

elixir phoenix Layoutの中でLayoutを出力する

Phoenixで初期生成されるLayout

Phoenixフレームワークで初期生成されるLayout lib/yourapp_web/templates/layout/app.html.eex があります。このLayout内の main タグ内に各ページのコンテンツを出力していきます。このLayout内に別のLayoutを出力していきます

app.html.eex の中で added_layout.html.eex Layoutを出力する

app.html.eex の main タグ内で以下のように <%= render("added_layout.html") %> を追加します

    <main role="main" class="mainContainer">
        <%= @inner_content %>
        <%= render("added_layout.html") %> <!-- 追加 -->
    </main>

libyourapp_web emplateslayoutadded_layout.html.eex を作成します。中身は単純な文字列 "added_layout" を表示させます。

added_layout

以上です。

リファレンスには <%= render(HelloWeb.PageView, "test.html", message: "Hello from layout!") %> となっており、Layoutの他にViewモジュールとデータを渡すことが可能です。

参考資料