简体   繁体   English

在Rails中显示/隐藏零件/模板的更干燥的方法

[英]A more DRY way to show/hide partials/template in rails

Usually the recommended solution is content_for in the parent layout file. 通常,推荐的解决方案是父布局文件中的content_for But this results in having to specify it in every view since the default becomes not displaying it if the content is not present in the child view partial. 但这导致必须在每个视图中都指定它,因为如果子视图部分中不存在内容,则默认值将不显示它。

For example, if I have a sidebar that I want to hide in authentication pages, but have it everywhere else, I now have to specify content of the sidebar in every view file except the authentication pages. 例如,如果我有一个侧边栏要隐藏在身份验证页面中,但在其他地方都没有,则现在必须在每个视图文件中指定除身份验证页面之外的侧边栏内容。 Not DRY. 不干。

This is much more cumbersome and not dry than before, where I could render a sidebar partial in the layouts and be done with it (but not have the benefit of choosing where it does or doesn't render). 这比以前麻烦得多,而且不干燥,在以前我可以在布局中渲染侧边栏的一部分并用它来完成(但没有选择在何处渲染或不渲染的好处)。

It would be nice if you could somehow specify in the layouts template that calls the sidebar partial to not tender if it's a session controller or devise controller. 如果您能以某种方式在布局模板中指定调用边栏parta而不是投标的会话控制器或devise控制器,那就太好了。

Is there any way to do this? 有什么办法吗? I've tried using 我试过使用

unless devise_controller?
  render "layouts/sidebar"

But this doesn't seem to work as intended. 但这似乎并没有达到预期的效果。


Taking an example with Devise, I have an application.html.slim file like so 以Devise为例,我有一个application.html.slim文件,像这样

.row
  main
    .col-sm-9
      = yield
    - unless devise_controller?
      .col-sm-3
        == render 'layouts/sidebar'

Ignore the columns for a moment. 暂时忽略列。 The main point is that the =yield renders normal templates as well as the devise templates. 要点是=yield渲染普通模板以及devise模板。 So I can't selectively disable the sidebar in one DRY stroke. 因此,我无法在一个DRY笔画中有选择地禁用边栏。 I would have to put the sidebar render call in every view file, but not include it in my devise views. 我将不得不在每个视图文件中添加侧边栏渲染调用,但不将其包含在我的设计视图中。 That's no better or DRYer than using content_for in every non-devise view file. 这与在每个非设计视图文件中使用content_for一样好,也没有DRYer。

And getting back to the columns, I would like to have the yielded devise views full width of 12 columns, not 9 columns. 回到各列,我希望产生的devise视图全宽为12列,而不是9列。 But that's just bonus, not the main question. 但这只是奖金,不是主要问题。

If you insist on checking the controller name you can do this: 如果您坚持检查控制器名称,则可以执行以下操作:

<% if controller.controller_name == 'devise' %>
...
<% else %>
  <%= render 'sidebar' %>
<% end %>

I simply implemented the verbose way of doing things. 我只是实现了冗长的处理方式。 I'll use a complex example since it might be more helpful. 我将使用一个复杂的示例,因为它可能会更有帮助。

Here, I set up the full cases, depending on whether I want a sidebar or not, mainly because of the way bootstrap columns and rows work. 在这里,我根据是否需要侧边栏设置了完整的案例,主要是因为引导列和行的工作方式。 If you're using bootstrap, you'll notice that once you prevent the sidebar from rendering, you'll be left with the rest of the main content not taking the space that the sidebar no longer takes. 如果您使用的是引导程序,则会注意到一旦阻止了边栏的呈现,您将剩下的其余主要内容不再占用边栏不再占用的空间。 This is because you previously had to put the sidebar and main content in a specific column width, and those in a row (so that they appear side by side). 这是因为您以前不得不将侧边栏和主要内容放置在特定的列宽中,而将它们放置在一行中(以便它们并排显示)。 Since you want the main content to fill and take the full width when the sidebar is gone, you have to apply completely different column widths in that case. 由于要在边栏消失时填充主要内容并采用整个宽度,因此在这种情况下,您必须应用完全不同的列宽。 Hence the full if - else statement. 因此,完整的if-else语句。

Thus, implement it like so, making sure to include .rows and .col-*-* in each of your main yielded view files. 因此,应像这样实现它,并确保在每个主要产生的视图文件中都包含.rows.col-*-*

application.html application.html

  - if hide_sidebar?
    main
      = yield
  - else
    .row
      .col-xs-12.col-sm-9
        main
          = yield
      .col-xs-12.col-sm-3
        == render 'layouts/sidebar'

If the list of pages you wish to have or not have a sidebar becomes more complex than simply all of the actions of a certain controller, implement a separate helper method specifying all the conditions. 如果您希望有或没有侧边栏的页面列表比仅某个控制器的所有操作复杂得多,请实施一个单独的帮助程序方法来指定所有条件。

This is one way to do it, in the controller. 这是在控制器中执行此操作的一种方法。 In my example, I want to hide the sidebar when it's the registrations controller and when it's the show action of the users controller. 在我的示例中,我想在侧边栏是注册控制器时并且在它是用户控制器的show动作时将其隐藏。 I just || 我只是|| each out in the helper method. 每个都在辅助方法中。

application_helper.rb application_helper.rb

def want_sidebar?
  (controller_name == 'registrations') || ( (controller_name == 'users') && action_name == 'show' )
end

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

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