免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1003 | 回复: 0
打印 上一主题 下一主题

All About Layouts [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-27 15:47 |只看该作者 |倒序浏览

                                1. layouts are files that shared across many actions and controllers.
They define the code that surrounds a template.
2. Application layouts
below is a template(a common web page code, we will add application layout later):
Projects
   
        
   
If we want to add a header, a logo, and some menu navigation to this site and have it visible on every page then we should use a layout.
layout files live in /app/views/layouts folder.
The name of the layout file indicate it is applying to which controller by default.
Create a application.html.erb in this directory, it will create a global layout which will be used by all controllers and all actions.
for example:
Application Layout!
look at the second row, yield tells the layout where to place the content for the template that is using the layout.
This layout will be auto-added to the formal template.
This layout will be added to every action in every controller across the app.
But what if we need different layouts for different parts of our app?
3. Controller-specific Layouts.
A layout can be made specific to a controller by giving it the name of the controller.
So, to make a controller that will be used by all of the actions in the Projects controller create a file in the layouts folder called projects.html.erb. This means that the layout will be used only by the projects controller.
What if we want to share a layout across a number of controllers, not just one, e.g. for an admin layout?
Rails allows you to use the layout command to specify the name of the layout that should be used wihtin a controller.
e.g.:
class ProjectsController   layout "admin"       def index      @projects = Project.find(:all)    end  end  
Remeber, the controller layout will overwrite the application layouts.And The layout specified with the layout command in controller will overide any controller-specific or application-specific layouts.
4. Dynamic layouts.
Layouts can also be used dynamically. We might only want the admin layout to be used when a user is logged in.
This can be done by passing a symbol argument to the layout command and creating a method with the same name as the symbol which determine which layout should be used.
e.g.:
class ProjectController render command.
e.g.
def index
    @projects = Project.find(:all)
    render :layout => 'projects'
end
   
The layout specified with the render command will override any controller-specific layout. To render an action with no layout we can use
render :layout => false
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/25407/showart_1912084.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP