调用 Rails 应用程序的主要“yield”从部分

发布于 2024-08-14 16:09:11 字数 1079 浏览 5 评论 0原文

我的问题很简单。如果答案很明显,我会先问它,然后如果答案不明显,我会解释我的意思。

从部分进行主要“yield”调用而不是直接从layout.html.haml 文件进行调用是否可以?这样做是否会导致任何类型的性能损失?


说明...

我有一个名为 application.html.haml 的布局文件。

我希望我的主要内容占据整个页面宽度,除非该页面有侧边栏。

如果有侧边栏,那么我希望主要内容占据 66% 的页面宽度,侧边栏占用剩余空间。

我的布局中有以下内容:

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

内容 div 为 100% 页面宽度。如果没有侧边栏,则“产量”结果将进入此 div。如果有侧边栏,则主要产量将进入名为 #main 的 div,其页面宽度为 66%。

太好了,这很好用。

现在,为了保持主视图整洁,我对代码进行了一些重构,现在看起来像这样:

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

然后在部分 _main_with_sidebar.html.haml 中我有这样的:

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

所以一切都是相同的,除了每当存在侧边栏时,都会从部分调用主收益。

所以我的问题是这是否被认为是最佳实践。或者我应该坚持使用稍微混乱的 application.html.haml 文件并删除部分内容?它似乎不会造成任何问题,但我想知道我是否做错了什么,然后再走得太远。

是的,这对你们中的一些人来说可能看起来很愚蠢,但我是一名设计师而不是开发人员,这种事情对我来说是新的......

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? Does doing so result in any kind of performance loss.


Explanation...

I have one layout file called 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.

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. If no sidebar is present then the 'yield' results go into this div. If there is a sidebar then the main yield goes into a div called #main which is 66% page width.

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
      = 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? 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....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀梦 2024-08-21 16:09:11

我只需为“带侧边栏的布局”创建一个新布局

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

I would simply create a new layout for the "layout with sidebar"

views/layouts/application.html.haml
views/layouts/sidebar.html.haml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文