在Phoenix中对模板使用不同布局的正确方法 [英] Proper way to use different Layouts for Templates in Phoenix

查看:58
本文介绍了在Phoenix中对模板使用不同布局的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每个Controller动作中更改模板布局以使用put_layout方法的正确/最简单的方法吗?一个简单的示例,希望为不同的控制器使用不同的布局似乎变得非常重复(如下所示),因此感觉好像我在框架中缺少某些东西.

Is the proper/simplest way to change the Layout of a Template to use the put_layout method within each Controller action? A simple example of wanting a different Layout for different Controllers seems to become very repetitive (below) so it feels like I'm missing something within the framework.

defmodule MyPhoenix.AController do 
    use MyPhoenix.Web, :controller

    def pageOne(conn, _params) do
        conn
        |> put_layout("LayoutA.html")
        |> render "page1.html" 
    end

    def pageTwo(conn, _params) do 
        conn
        |> put_layout("LayoutA.html")
        |> render "page2.html" 
    end
end

defmodule MyPhoenix.BController do 
    use MyPhoenix.Web, :controller

    def pageOne(conn, _params) do
        conn
        |> put_layout("LayoutB.html")
        |> render "page1.html" 
    end

    def pageTwo(conn, _params) do 
        conn
        |> put_layout("LayoutB.html")
        |> render "page2.html" 
    end
end

推荐答案

我认为最好设置默认布局.

I think you might be best off by setting a default layout.

defmodule MyPhoenix.AController do 
    use MyPhoenix.Web, :controller

    plug :put_layout, "LayoutA.html"

    def pageOne(conn, _params) do
        render conn, "page1.html"
    end

    def pageTwo(conn, _params) do 
        render conn, "page2.html" 
    end
end

defmodule MyPhoenix.BController do 
    use MyPhoenix.Web, :controller

    plug :put_layout, "LayoutB.html"

    def pageOne(conn, _params) do
        render conn, "page1.html" 
    end

    def pageTwo(conn, _params) do 
        render conn, "page2.html"
    end
end

这篇关于在Phoenix中对模板使用不同布局的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆