两部分 Rails 布局
我的网页由两部分组成,比如说顶部和底部(页眉和页脚除外——这些部分在页面之间是一致的)。根据操作动态生成这些部分的最佳实践是什么?
我想出的一种方法是顶部视图和底部部分视图;在布局中,顶部调用yield,底部调用renderpartial。部分的名称根据操作动态替换。
不确定这是最好的方法。
My web pages consist of two parts, let's say top and bottom (except header and footer -- those are consistent across pages). What is the best practice to dynamically generate those parts depending on the action?
One approach I have come up with is to have view for the top and partial for the bottom; in the layout call yield for the top and render partial for the bottom. The name of the partial is dynamically substituted depending on the action.
Not sure it is the best way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我觉得你的想法很好。在你看来你可以这样做:
当然你应该检查部分是否存在并提供一些默认行为。但我想你还是意识到了这一点。
然后在你的布局中:
I think your idea is fine. In your views you could do:
Of course you should check whether the partial exist and provide some default behavior. But I think you're aware of that anyway.
And then in your layout:
这是我过去使用过的视图 DSL 的一个非常简化的版本。对我们来说效果很好。实际上,我们参数化了辅助方法,这样我们就可以从许多布局部分中进行动态选择(使页面具有侧边栏、多列等)。
Here is a very simplified version of a view DSL I've used in the past. Worked well for us. In reality we parameterized the the helper methods so we could choose from many layout partials on the fly (to have pages with sidebars, multiple columns, etc).