layout详解
来源:互联网 发布:淘宝店铺装修图片 编辑:程序博客网 时间:2024/05/24 02:06
一般来说layout有如下五种:
gobal layout,
controller layout,
shared layout,
dynamic layout,
action layout
假设我们有一个views/projects/index.rhtml页面:
Java代码
- <h2>Projects</h2>
- <ul>
- <% for project in @projects %>
- <li><%= project.name %></li>
- <% end %>
- </ul>
<h2>Projects</h2><ul><% for project in @projects %> <li><%= project.name %></li><% end %></ul>
下面来看看各种layout的用法。
1,global layout
添加views/layouts/application.rhtml:
Java代码
- <h1>Application Layout!</h1>
- <%= yield %>
<h1>Application Layout!</h1><%= yield %>
在layouts目录下添加application.rhtml即可,<%= yield %>即输出我们的projects/index.rhtml页面
由于我们的controller都继承自ApplicationController,所以application.rhtml会先解析
2,controller layout
添加views/layouts/projects.rhtml:
Java代码
- <h1>Projects Layout!</h1>
- <%= yield %>
<h1>Projects Layout!</h1><%= yield %>
道理同上,ProjectsController当然会使用同名的projects.rhtml作layout了
注意的是controller layout会覆盖global layout
3,shared layout
添加views/layouts/admin.rhtml:
Java代码
- <h1>Admin Layout!</h1>
- <%= yield %>
<h1>Admin Layout!</h1><%= yield %>
我们建立了admin layout,然后在需要使用该layout的controller中指定即可:
Java代码
- class ProjectsController < ApplicationController
- layout "admin"
- def index
- @projects = Project.find(:all)
- end
- end
class ProjectsController < ApplicationController layout "admin" def index @projects = Project.find(:all) endend
4,dynamic layout
有时候我们需要根据不同的用户角色来使用不同的layout,比如管理员和一般用户,比如博客换肤(也可以用更高级的theme-generator)
Java代码
- 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
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 endend
5,action layout
在action中指定layout即可:
Java代码
- 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
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 endend
上面的index方法指定使用projects layout,当然我们也可以指定不使用layout,如printable页面:
Java代码
- def index
- @projects = Project.find(:all)
- render :layout => false
- end
def index @projects = Project.find(:all) render :layout => falseend
需要注意的是,这5种layout会按顺序后面的覆盖前面的layout
- layout详解
- layout详解
- Tab Layout Tutorials 详解
- IE layout详解
- Layout过程详解
- Extjs4 布局 layout 详解
- Android - 布局(layout) 详解
- Android 布局Layout详解
- Android Layout属性详解
- Auto Layout入门详解
- java layout详解
- iOS8 Layout Margins 详解
- Extjs4 布局 layout 详解
- Layout参数详解
- IE的layout属性详解
- Android之Table Layout详解
- ExtJS layout的样式详解
- Android中layout过程详解
- web-harvest 采集"腾讯读书“ 小说
- HDU 1712 分组背包
- H.264笔记
- 用lucene3.6搜索数据库和txt文件内容
- nginx evnent_posted 线程处理函数
- layout详解
- Android学习-使用单例模式实现一键退出APP
- h264的码率控制 JVT-G012
- VB工程15---百例28---图像缩放
- H264学习方法&&历程&&资料
- 舍与得一种境界,一种人生
- Flash 之死
- mysql select in
- wireshark解析rtp协议,流媒体中的AMR/H263/H264包的方法