原创作者: hideto   阅读:1114次   评论:0条   更新时间:2011-05-26    
这次来看看怎样为每个页面创建良好的title

为了让每个页面显示不同的title,我们需要在global layout -- application中留出位置:
<html>
  <head>
    <title>Shoppery - <%= yield(:title) || "The Place to Buy Stuff" %></title>
    <%= stylesheet_link_tag 'application' %>
    <%= yield :head %>
  </head>
  <body>
    <div id="container">
      <p style="color: green"><%= flash[:notice] %></p>
      <h1><%= yield(:title) %></h1>
      <%= yield %>
    </div>
  </body>
</html>

我们使用yield(:title)来显示具体每个页面的title或者默认title,并且用h1在页面中显示当前title

再看我们定义的一个全局helper方法:
module ApplicationHelper
  def title(page_title)
    content_for(:title) { page_title }
  end
end

我们定义一个title方法,并在方法里使用content_for来将page_title作为yield(:title)的返回值

好了,对具体每个页面我们调用title方法设置page_title即可:
<% title "New Product" %>

...
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics