简体   繁体   English

为页面标题编写辅助方法

[英]Writing a helper method for page headers

I am currently integrating twitter bootstrap. 我目前正在集成Twitter Bootstrap。

My application layout currently includes my flash messages right above the yield: 我的应用程序布局当前在收益率上方包括我的Flash消息:

    <div class="container">

            <%= render 'layouts/header' %>

            <div class="content" role="main">
                <%= render :partial => 'shared/flash', :object => flash %>
                <%= yield %>
            </div>

            <%= render 'layouts/footer' %>


    </div> <!--! end of #container -->

However, I am also using the bootstrap Page Headers in my individual views 但是,我也在个人视图中使用引导页面标题

    <div class="page-header">
        <h1>Sign In <small>through one of these services:</small></h1>
    </div>

I would like my flash messages to be below these page headers when page headers are set. 设置页面标题后,我希望我的Flash消息在这些页面标题下方。 So I'm thinking the best way to do this is to create a helper method and edit my application layout to be something like: 因此,我认为最好的方法是创建一个辅助方法并将应用程序布局编辑为:

        <%= render 'layouts/header' %>

        <div class="content" role="main">
            <%= pageHeader %>
            <%= render :partial => 'shared/flash', :object => flash %>
            <%= yield %>
        </div>

How would I go about writing the helper for this. 我将如何为此编写助手。 Would the best way to just have instance variables for @page_header and @page_header_small in my controller? 在控制器中只为@page_header和@page_header_small设置实例变量的最佳方法是吗? Or is there a better rails way to handle this situation? 还是有更好的方法来处理这种情况?

You can user content_for combined with yield. 您可以将content_for与yield结合使用。

In your layout you can put this block with headline above flash message 在您的布局中,您可以将此标题为标题的块放在Flash消息上方

<%= yield :page_header %>
<%= render :partial => 'shared/flash', :object => flash %>
<%= yield %>

and in your template for action 并在您的操作模板中

<% content_for :page_header do %>
  <div class="page-header">
    <h1>Sign In <small>through one of these services:</small></h1>
  </div>
<% end %>

Check it out at Ruby Guides Ruby Guides上查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM