Mat*_*ner 10 elixir phoenix-framework
我想title在凤凰城的子视图/控制器中设置应用程序模板中的标记.
该title标签是内部的web/templates/layout/app.html.eex模板,但我有一个ArticlesController这使得在<%= @inner %>Rails中来的时候,会用yield电话,但无法找到其在凤凰城等价的.
将属性从其子项传递到父模板/视图的正确方法是什么?
Gaz*_*ler 10
你有几个选择.我假设你想要像content_forrails中的东西.
一种选择是使用render_existing/3 http://hexdocs.pm/phoenix/0.14.0/Phoenix.View.html#render_existing/3
另一种灵活的方法是使用插头:
defmodule MyApp.Plug.PageTitle do
def init(default), do: default
def call(conn, opts) do
assign(conn, :page_title, Keyword.get(opts, :title)
end
end
Run Code Online (Sandbox Code Playgroud)
然后在你的控制器中你可以做到
defmodule FooController do
use MyApp.Web, :model
plug MyApp.Plug.PageTitle, title: "Foo Title"
end
defmodule BarController do
use MyApp.Web, :controller
plug MyApp.Plug.PageTitle, title: "Bar Title"
end
Run Code Online (Sandbox Code Playgroud)
在你的模板中;
<head>
<title><%= assigns[:page_title] || "Default Title" %></title>
</head>
Run Code Online (Sandbox Code Playgroud)
这里我们使用assigns而不是@page_title因为@page_title如果没有设置值将会提高.