【发布时间】:2008-10-09 04:53:04
【问题描述】:
在不使用插件的情况下,在 Rails 应用程序中为页面创建自定义标题的最佳方法是什么?
【问题讨论】:
标签: ruby-on-rails ruby
在不使用插件的情况下,在 Rails 应用程序中为页面创建自定义标题的最佳方法是什么?
【问题讨论】:
标签: ruby-on-rails ruby
在你的观点中做这样的事情:
<% content_for :title, "Title for specific page" %>
<!-- or -->
<h1><%= content_for(:title, "Title for specific page") %></h1>
布局文件中包含以下内容:
<head>
<title><%= yield(:title) %></title>
<!-- Additional header tags here -->
</head>
<body>
<!-- If all pages contain a headline tag, it's preferable to put that in the layout file too -->
<h1><%= yield(:title) %></h1>
</body>
也可以将content_for 和yield(:title) 语句封装在辅助方法中(正如其他人已经建议的那样)。但是,在这种简单的情况下,我喜欢将必要的代码直接放入特定的视图中,而不需要自定义帮助器。
【讨论】:
<title><%= (yield(:title) + " - " unless yield(:title).blank?).to_s + "This is always shown" %></title> 和视图中的<% content_for :title, "Conditional title" %>。
<h1><%= content_for(:title, "Title for specific page") %></h1> 时,我看不到任何输出到我的 DOM 中(即我有 <h1></h1>,其中没有内容)。但是我的<title>Title for specific page</title> 工作正常。有什么想法吗?
content_for 可能已经发生了变化。我必须使用两个单独的行:<% content_for :title, 'The Title' %> 来设置标题,然后是 <h1><%= content_for :title %></h1> 来显示它。
<title><%= yield(:title).presence || 'default_title' %></title> 添加默认标题
这是一个我喜欢使用的简单选项
在你的布局中
<head>
<title><%= @title %></title>
</head>
在页面模板的顶部(第一行)
<% @title="Home" %>
由于布局和页面模板的解析方式,@title="Home" 会在布局呈现之前进行评估。
【讨论】:
@opsb 的改进和 @FouZ 的更完整形式:
在application.html.erb:
<title><%= @title || "Default Page Title" %></title>
在视图erb文件或其控制器中:
<% @title = "Unique Page Title" %>
【讨论】:
这种简单的方法设置了默认标题,但也可以让您随时覆盖它。
在app/views/layouts/application.html.erb:
<title><%= yield(:title) || 'my default title' %></title>
要覆盖该默认值,请将其放置在您喜欢的任何视图中
<% content_for :title, "some new title" %>
【讨论】:
yield(:title).presence,因此如果接收者不存在,它会返回 nil。以防万一它不适用于其他人。
最佳做法是使用 content_for。
首先,添加几个辅助方法(即,粘贴在 app/helpers/application_helper.rb 中):
def page_title(separator = " – ")
[content_for(:title), 'My Cool Site'].compact.join(separator)
end
def page_heading(title)
content_for(:title){ title }
content_tag(:h1, title)
end
然后在您的布局视图中,您可以简单地使用:
<title><%= page_title %></title>
...在视图本身中:
<%= page_heading "Awesome" %>
这种方式的优点是可以让你在标题中粘贴 h1 标签的位置随机播放,并且让你的控制器保持美观且没有讨厌的@title 变量。
【讨论】:
@content_for_title的值?
@content_for_title 是什么意思。应该是(content_for(:title) + ' &mdash; ' if content_for?(:title)).to_s + 'My Cool Site'。然后从您的视图中设置它,只需 <% content_for :title, 'My Page Title' %>
@content_for_title 是您在撰写此评论时使用 content_for 时发生的情况。我相信情况不再如此,但我最近没有看过。 Toby J 的评论对于今天的 Rails 来说是正确的。我会更新我原来的答案。
使用 content_for 方法和部分标题的方法
1 .部分名称:_page_title.html.erb
<%content_for :page_title do %>
<%=title%>
<%end%>
2 。在title标签内的application.html.erb中使用
<title><%=yield :page_title %></title>
3 .各自 *.html.erb 中的用法,不包括 application.html.erb 只需将其粘贴在任何 .html.erb 中并提供页面标题
e.g : inside index.html.erb
<%=render '_page_title', title: 'title_of_page'%>
【讨论】:
最好的/干净的方法:
<title><%= @page_title or 'Page Title' %></title>
【讨论】:
查看content_for:http://railscasts.com/episodes/8
【讨论】:
如果没有关于您要满足的用例或要求的更多详细信息,我可以想到几个替代方案:
1) 在您的一个布局页面中切换标题并使用存储在application_helper.rb 中的辅助方法
<title><%= custom_title %></title>
这种方法将为您提供每个布局页面的唯一标题。
2) Railscasts 建议使用部分加载 HEAD 标签之间显示的内容
3) 如果您需要在加载事件后更改标题,请使用 javascript/ajax 调用来操作 DOM。
也许您并不想更改title 元素标记的内容。也许您真的需要某种面包屑,以便您的用户始终知道他们在您网站的导航层次结构中的位置。虽然我对 goldberg 插件的操作做得很好,但我确信还有其他方法可以实现相同的功能。
【讨论】:
我使用 nifty_generator 的“nifty_layout”,它提供了一个标题变量,然后我可以在页面上使用:
<% title "Title of page" %>
我也可以使用<% title "Title of page", false %> 让标题只显示在浏览器标题中,而不是页面本身。
【讨论】:
您也可以在控制器的 before_filter 中设置它。
# foo_controller.rb
class FooController < ApplicationController
before_filter :set_title
private
def set_title
@page_title = "Foo Page"
end
end
# application.html.erb
<h1><%= page_title %></h1>
然后您可以在 set_title 方法中设置条件,为控制器中的不同操作设置不同的标题。很高兴能够在您的控制器中看到所有相关的页面标题。
【讨论】:
我使用 这个插件 我写的这些 Rails 助手:http://web.archive.org/web/20130117090719/http://henrik.nyh.se/2007/11/my-rails-title-helpers/
【讨论】:
简而言之,我可以这样写
<%content_for :page_title do %><%= t :page_title, "Name of Your Page" %> <% end %>
【讨论】:
我喜欢 opsb 的方法,不过这个方法也行。
<% provide(:title,"ttttttttttttttttttZ") %>
<html>
<head><title><%= yield(:title) %></title></head>
<body></body>
</html>
【讨论】:
我想确保始终为当前页面提供翻译。
这是我在app/views/layouts/application.html.erb 中想到的:
<title>
<%= translate("#{controller_name}.#{action_name}.site_title", raise: true) %>
</title>
这将始终为当前视图寻找翻译,如果没有找到则引发错误。
如果你想要一个默认值,你可以这样做:
<title>
<%= translate("#{controller_name}.#{action_name}.site_title",
default: translate(".site_title")) %>
</title>
【讨论】:
我想添加我非常简单的变体。
在ApplicationController中定义这个方法:
def get_title
@action_title_name || case controller_name
when 'djs'
'Djs'
when 'photos'
'Photos'
when 'events'
'Various events'
when 'static'
'Info'
when 'club'
'My club'
when 'news'
'News'
when 'welcome'
'Welcome!'
else
'Other'
end
end
之后,您可以从布局的标题标签中调用 get_title。您可以通过在您的操作中定义@action_title_name 变量来为您的页面定义更具体的标题。
【讨论】: