对于 Ruby 和 Rails,当只使用一次时是否应该创建分部视图?
我正在考虑只使用一次的部分视图,例如页面页眉和页脚以及页眉中的选项卡,它们可以全部嵌入到应用程序布局中,并且所有页面都会有它们。
所以这就像编程——如果某件事只完成一次,可能不会创建一个函数来完成它,以避免开销。但在某些情况下,函数可能更加独立、模块化,并使代码更清晰易懂。在这种情况下,我们可能会创建该函数。
对于部分内容怎么样?是否应该创建它们,例如页眉和页脚、选项卡或侧边栏选择。我也在想,有一天,如果有一个页面不包含页眉但有页脚,那么页眉就需要做成部分,那么为什么不在第一天就这样做呢?但是,过多的部分会减慢页面生成的开销,这会如何影响呢?
更新:还存在太多部分的危险...其中视图包含部分,部分包含部分,然后再次包含部分。此时,很容易就会有些失落。
I am thinking for partial view that is only used once, such as page header and footer, and the tabs in the header, they can be all embedded in the application layout, and all pages will have them.
So it is like programming -- if something is done only once, probably don't create a function to do it, to avoid overheads. But there might be situations where a function is more self-contained, modular, and make the code more clear to understand. In that case, we probably will create the function.
How about for partials? Should they be created, such as for header and footer, tabs, or sidebar selections. I am also thinking that when one day, if there is a page that doesn't include the header but has the footer, then the header will need to be made into a partial, so why not do it on day 1? But how does it impact the overhead of too many partials slowing down the page generation?
update: there is also the danger of too many partials... where a view includes a partial, and a partial includes a partial, and then again includes a partial. At this point, it is easy to be a bit lost.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我总是创建这些部分,因为我想将这些东西放在不同的地方。例如,页眉部分和页脚部分是两个独立的东西,都以 div 标签开头并以 div 标签结束。对我来说,将它们放在单独的文件中更有意义。您可以使布局更加干净。
在 Rails 1 和 2 中,局部渲染有点慢(甚至没有那么慢)。在 Rails 3 中,渲染局部渲染要快得多。
I always create those partials because i would like to have those things in seperate places. For example a Header partial and a footer partial are 2 seperate things both beginning with a div tag and closing with a div tag. For me it makes much more sense to have them in a seperate file. You keep your layout much cleaner.
In Rails 1 and 2 partials were a little slow (not even that much) In Rails 3 rendering partials is much faster.
我认为包含部分内容的开销可以忽略不计。
如果它使你的代码更整洁/更有组织/面向未来,那么我说就这样做。
I would think the overheads of including a partial are negligible.
If it makes your code neater / more organised / future-proof then I say do it.