简体   繁体   English

从局部调用Rails应用的主要“收益”

[英]Calling a rails app's main 'yield' from a partial

My question is very simple. 我的问题很简单。 I will ask it first in case the answer is obvious, and then I will explain what I mean, in case it's not obvious. 如果答案很明显,我将首先询问,然后,如果答案不明显,我将解释我的意思。

Is it considered ok to make your main 'yield' call from a partial instead of doing it directly from your layout.html.haml file? 是否可以通过部分调用而不是直接从layout.html.haml文件进行主要的“屈服”调用来实现? Does doing so result in any kind of performance loss. 这样做会导致任何形式的性能损失。


Explanation... 说明...

I have one layout file called application.html.haml. 我有一个名为application.html.haml的布局文件。

I want my main content to take up the full page width unless there is a sidebar present for that page. 我希望我的主要内容占据整个页面的宽度,除非该页面没有侧边栏。

If there is a sidebar then I want the main content to take up 66% page width and the sidebar to use up the remaining space. 如果有侧边栏,那么我希望主要内容占用66%的页面宽度,并希望侧边栏占用剩余的空间。

I have the following in my layout: 我的布局中包含以下内容:

#content
  - if show_sidebar?    
    #main
      = yield
    #sidebar
      = yield(:sidebar)
  -else
    = yield

The content div is 100% page width. 内容div是100%页面宽度。 If no sidebar is present then the 'yield' results go into this div. 如果没有侧边栏,则“收益”结果进入该div。 If there is a sidebar then the main yield goes into a div called #main which is 66% page width. 如果有侧边栏,则主要产量进入称为#main的div中,即页面宽度的66%。

Great, this works fine. 很好,这很好。

Now, in order to keep my main view tidy, I have refactored my code a little so that it now looks like this: 现在,为了使主视图保持整洁,我对代码进行了一些重构,使其看起来像这样:

#content
  - if show_sidebar?
    - render :partial => 'main_with_sidebar'
  -else
    = yield

And then in the partial _main_with_sidebar.html.haml I have this: 然后在部分_main_with_sidebar.html.haml我有以下内容:

    #main
      = yield
    #sidebar
      = yield(:sidebar)

So everything is the same, except for the fact that whenever there is a sidebar present, the main yield is called from a partial. 因此,所有情况都是相同的,除了以下事实:只要存在侧边栏,就会从局部调用主收益。

And so my question regards whether or not this is considered best practice. 因此,我的问题是关于这是否被视为最佳实践。 Or should I just stick to having a slightly messier application.html.haml file and get rid of the partial? 还是我应该坚持使用稍微有些混乱的application.html.haml文件并摆脱部分文件? It doesn't seem to cause any problems but I'd like to know if I'm doing something wrong before I go too far with it. 它似乎没有引起任何问题,但是我想知道在我做错什么之前,我是否做错了什么。

Yes it might seem dumb to some of you guys, but I'm a designer rather than a dev and this sort of thing is new to me.... 是的,对于你们中的某些人来说似乎有些愚蠢,但是我是设计师而不是开发人员,而这件事对我来说是新的。

I would simply create a new layout for the "layout with sidebar" 我只需为“带有侧边栏的布局”创建一个新的布局

views/layouts/application.html.haml
views/layouts/sidebar.html.haml

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

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