主题:Rails宝典之第七式: layout详解
来源:互联网 发布:nginx server配置 编辑:程序博客网 时间:2024/06/09 22:31
这次的视频很有用,详细解释了layout的用法
一般来说layout有如下五种:
gobal layout,controller layout,shared layout,dynamic layout,action layout
假设我们有一个views/projects/index.rhtml页面:
下面来看看各种layout的用法。
1,global layout
添加views/layouts/application.rhtml:
在layouts目录下添加application.rhtml即可,<%= yield %>即输出我们的projects/index.rhtml页面
由于我们的controller都继承自ApplicationController,所以application.rhtml会先解析
2,controller layout
添加views/layouts/projects.rhtml:
道理同上,ProjectsController当然会使用同名的projects.rhtml作layout了
注意的是controller layout会覆盖global layout
3,shared layout
添加views/layouts/admin.rhtml:
我们建立了admin layout,然后在需要使用该layout的controller中指定即可:
4,dynamic layout
有时候我们需要根据不同的用户角色来使用不同的layout,比如管理员和一般用户,比如博客换肤(也可以用更高级的theme-generator)
5,action layout
在action中指定layout即可:
上面的index方法指定使用projects layout,当然我们也可以指定不使用layout,如printable页面:
需要注意的是,这5种layout会按顺序后面的覆盖前面的layout
关于erb和capture的文章:http://hideto.iteye.com/blog/97353
一般来说layout有如下五种:
gobal layout,controller layout,shared layout,dynamic layout,action layout
假设我们有一个views/projects/index.rhtml页面:
- <h2>Projects</h2>
- <ul>
- <% for project in @projects %>
- <li><%= project.name %></li>
- <% end %>
- </ul>
下面来看看各种layout的用法。
1,global layout
添加views/layouts/application.rhtml:
- <h1>Application Layout!</h1>
- <%= yield %>
在layouts目录下添加application.rhtml即可,<%= yield %>即输出我们的projects/index.rhtml页面
由于我们的controller都继承自ApplicationController,所以application.rhtml会先解析
2,controller layout
添加views/layouts/projects.rhtml:
- <h1>Projects Layout!</h1>
- <%= yield %>
道理同上,ProjectsController当然会使用同名的projects.rhtml作layout了
注意的是controller layout会覆盖global layout
3,shared layout
添加views/layouts/admin.rhtml:
- <h1>Admin Layout!</h1>
- <%= yield %>
我们建立了admin layout,然后在需要使用该layout的controller中指定即可:
- class ProjectsController < ApplicationController
- layout "admin"
- def index
- @projects = Project.find(:all)
- end
- end
4,dynamic layout
有时候我们需要根据不同的用户角色来使用不同的layout,比如管理员和一般用户,比如博客换肤(也可以用更高级的theme-generator)
- class ProjectsController < ApplicationController
- layout :user_layout
- def index
- @projects = Project.find(:all)
- end
- protected
- def user_layout
- if current_user.admin?
- "admin"
- else
- "application"
- end
- end
- end
5,action layout
在action中指定layout即可:
- class ProjectsController < ApplicationController
- layout :user_layout
- def index
- @projects = Project.find(:all)
- render :layout => 'projects'
- end
- protected
- def user_layout
- if current_user.admin?
- "admin"
- else
- "application"
- end
- end
- end
上面的index方法指定使用projects layout,当然我们也可以指定不使用layout,如printable页面:
- def index
- @projects = Project.find(:all)
- render :layout => false
- end
需要注意的是,这5种layout会按顺序后面的覆盖前面的layout
关于erb和capture的文章:http://hideto.iteye.com/blog/97353
- 主题:Rails宝典之第七式: layout详解
- Rails宝典之第七式: layout详解
- 2007-07-29Rails宝典之第七式: layout详解
- Rails宝典之第八式: layout与content_for
- Rails宝典之第五式: 使用with_scope
- Rails宝典之第十五式: find条件
- Rails宝典之第十六式: 虚拟属性
- Rails宝典之第十八式: 循环flash
- Rails宝典之第二十式: 限制访问
- Rails宝典之第二十二式: eager loading
- Rails宝典之第二十三式: counter cache
- Rails宝典之第二式: 动态find_by方法
- Rails宝典之第三式: 通过关联做查询
- Rails宝典之第六式: Symbol to Proc
- Rails宝典之第九式: 在日志里过滤敏感数据
- Rails宝典之第十式: 重构用户名-p1
- Rails宝典之第十二式: 重构用户名-p3
- Rails宝典之第十七式: 多对多Checkbox编辑
- 条款一:Distinguish between pointers and references
- 我的第一个CSDN博客——找实习 到 实习 的一些感想
- Doom3 CVarSystem分析
- UE快捷键
- 有关ubuntu下面更新后有线不能上网的问题的解决方法
- 主题:Rails宝典之第七式: layout详解
- PHP和mysql做用户登录验证 .
- 实现PHP用户的注册与登录 .
- ajax检测用户名 .
- 协议编码压缩
- viewWillAppear:等无法调用的总结
- MACBOOK 连接不上wifi的解决办法
- 从毕业开始工作
- Team Leader你会带团队吗?深刻理解团队合作以及原理