博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
layout详解
阅读量:4042 次
发布时间:2019-05-24

本文共 2493 字,大约阅读时间需要 8 分钟。

一般来说layout有如下五种:

gobal layout,

controller layout,

shared layout,

dynamic layout,

action layout

假设我们有一个views/projects/index.rhtml页面:

Java代码

 

  1. <h2>Projects</h2>   
  2. <ul>   
  3. <% for project in @projects %>   
  4.   <li><%= project.name %></li>   
  5. <% end %>   
  6. </ul>  

 

<h2>Projects</h2><ul><% for project in @projects %> <li><%= project.name %></li><% end %></ul>

下面来看看各种layout的用法。

1,global layout

添加views/layouts/application.rhtml:

Java代码 

  1. <h1>Application Layout!</h1>   
  2. <%= 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代码

  1. <h1>Projects Layout!</h1>   
  2. <%= yield %>  

<h1>Projects Layout!</h1><%= yield %>

道理同上,ProjectsController当然会使用同名的projects.rhtml作layout了

注意的是controller layout会覆盖global layout

3,shared layout

添加views/layouts/admin.rhtml:

Java代码

 

  1. <h1>Admin Layout!</h1>   
  2. <%= yield %>  

 

<h1>Admin Layout!</h1><%= yield %>

我们建立了admin layout,然后在需要使用该layout的controller中指定即可:

Java代码

 

  1. class ProjectsController < ApplicationController   
  2.   layout "admin"  
  3.   
  4.   def index    
  5.     @projects = Project.find(:all)   
  6.   end   
  7. end  

class ProjectsController < ApplicationController layout "admin" def index @projects = Project.find(:all) endend

4,dynamic layout

有时候我们需要根据不同的用户角色来使用不同的layout,比如管理员和一般用户,比如博客换肤(也可以用更高级的)

Java代码

 

  1. class ProjectsController < ApplicationController   
  2.   layout :user_layout   
  3.   
  4.   def index   
  5.     @projects = Project.find(:all)   
  6.   end   
  7.   
  8.   protected  
  9.   
  10.   def user_layout   
  11.     if current_user.admin?   
  12.       "admin"  
  13.     else  
  14.       "application"  
  15.     end   
  16.   end   
  17. 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代码

 

  1. class ProjectsController < ApplicationController   
  2.   layout :user_layout   
  3.   
  4.   def index   
  5.     @projects = Project.find(:all)   
  6.     render :layout => 'projects'  
  7.   end   
  8.   
  9.   protected  
  10.   
  11.   def user_layout   
  12.     if current_user.admin?   
  13.       "admin"  
  14.     else  
  15.       "application"  
  16.     end   
  17.   end   
  18. 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代码

  1. def index   
  2.   @projects = Project.find(:all)   
  3.   render :layout => false  
  4. end  

def index @projects = Project.find(:all) render :layout => falseend

需要注意的是,这5种layout会按顺序后面的覆盖前面的layout 

转载地址:http://ndadi.baihongyu.com/

你可能感兴趣的文章
Oracle执行语句跟踪(1)——使用sql trace实现语句追踪
查看>>
oralce 18c 创建PDB几种方式
查看>>
oracle exp 导出表时会发现少表,空表导不出解决方案
查看>>
ORA-14450:试图访问已经在使用的事务处理临时表
查看>>
ORACLE RMAN 各种场景恢复
查看>>
oracle 自动导出package/package body/procedure 等为sql文件并且自动上传到ftp服务器上
查看>>
linux 下 su - oracle 切换不了
查看>>
初学MonggoDb—Linux平台安装MongoDB
查看>>
使用“rz -be”命令上传文件至服务器;使用“sz 文件名”从服务器下载文件到本地
查看>>
mysql:pt-online-schema-change 在线修改表
查看>>
oracle 调整表空间大小 (resize)
查看>>
Oracle scn健康状态检查脚本—scnhealthcheck.sql
查看>>
Redis+TwemProxy(nutcracker)集群部署测试
查看>>
Docker mysql8.0 oltp 性能测试
查看>>
oracle 只开启一个监听端口,却多个端口都可以连接
查看>>
ORA-00600: internal error code, arguments: [qcisSetPlsqlCtx:tzi init], [], [],
查看>>
CAP理论被应用在何方?
查看>>
RocketMQ集群安装部署
查看>>
kafka搭建入门
查看>>
高并发秒杀系统的设计思考!
查看>>