在 Phoenix 中为模板使用不同布局的正确方法
Proper way to use different Layouts for Templates in Phoenix
proper/simplest 更改模板布局以在每个控制器操作中使用 put_layout
方法的方法吗?想要为不同的控制器使用不同的布局的一个简单示例似乎变得非常重复(如下),所以感觉我在框架内遗漏了一些东西。
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
我认为您最好设置默认布局。
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
例如,如果您需要不同的布局来表示路由器中单独的管理管道覆盖的所有管理控制器,您可以为管理管道指定 plug :put_layout, {MyApp.LayoutView, :admin}
。我是从http://www.cultivatehq.com/posts/how-to-set-different-layouts-in-phoenix/.
那里学来的
proper/simplest 更改模板布局以在每个控制器操作中使用 put_layout
方法的方法吗?想要为不同的控制器使用不同的布局的一个简单示例似乎变得非常重复(如下),所以感觉我在框架内遗漏了一些东西。
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
我认为您最好设置默认布局。
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
例如,如果您需要不同的布局来表示路由器中单独的管理管道覆盖的所有管理控制器,您可以为管理管道指定 plug :put_layout, {MyApp.LayoutView, :admin}
。我是从http://www.cultivatehq.com/posts/how-to-set-different-layouts-in-phoenix/.