这次的视频很有用,详细解释了layout的用法
一般来说layout有如下五种:
gobal layout,controller layout,shared layout,dynamic layout,action layout
假设我们有一个views/projects/index.rhtml页面:
[code]
<h2>Projects</h2>
<ul>
<% for project in @projects %>
<li><%= project.name %></li>
<% end %>
</ul>
[/code]
下面来看看各种layout的用法。
[b]1,global layout[/b]
添加views/layouts/application.rhtml:
[code]
<h1>Application Layout!</h1>
<%= yield %>
[/code]
在layouts目录下添加application.rhtml即可,<%= yield %>即输出我们的projects/index.rhtml页面
由于我们的controller都继承自ApplicationController,所以application.rhtml会先解析
[b]2,controller layout[/b]
添加views/layouts/projects.rhtml:
[code]
<h1>Projects Layout!</h1>
<%= yield %>
[/code]
道理同上,ProjectsController当然会使用同名的projects.rhtml作layout了
注意的是controller layout会覆盖global layout
[b]3,shared layout[/b]
添加views/layouts/admin.rhtml:
[code]
<h1>Admin Layout!</h1>
<%= yield %>
[/code]
我们建立了admin layout,然后在需要使用该layout的controller中指定即可:
[code]
class ProjectsController < ApplicationController
layout "admin"
def index
@projects = Project.find(:all)
end
end
[/code]
[b]4,dynamic layout[/b]
有时候我们需要根据不同的用户角色来使用不同的layout,比如管理员和一般用户,比如博客换肤(也可以用更高级的[url=http://rubyforge.org/projects/theme-generator/]theme-generator[/url])
[code]
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
[/code]
[b]5,action layout[/b]
在action中指定layout即可:
[code]
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
[/code]
上面的index方法指定使用projects layout,当然我们也可以指定不使用layout,如printable页面:
[code]
def index
@projects = Project.find(:all)
render :layout => false
end
[/code]
需要注意的是,这5种layout会按顺序后面的覆盖前面的layout
关于erb和capture的文章:[url]http://hideto.iteye.com/blog/97353[/url]
一般来说layout有如下五种:
gobal layout,controller layout,shared layout,dynamic layout,action layout
假设我们有一个views/projects/index.rhtml页面:
[code]
<h2>Projects</h2>
<ul>
<% for project in @projects %>
<li><%= project.name %></li>
<% end %>
</ul>
[/code]
下面来看看各种layout的用法。
[b]1,global layout[/b]
添加views/layouts/application.rhtml:
[code]
<h1>Application Layout!</h1>
<%= yield %>
[/code]
在layouts目录下添加application.rhtml即可,<%= yield %>即输出我们的projects/index.rhtml页面
由于我们的controller都继承自ApplicationController,所以application.rhtml会先解析
[b]2,controller layout[/b]
添加views/layouts/projects.rhtml:
[code]
<h1>Projects Layout!</h1>
<%= yield %>
[/code]
道理同上,ProjectsController当然会使用同名的projects.rhtml作layout了
注意的是controller layout会覆盖global layout
[b]3,shared layout[/b]
添加views/layouts/admin.rhtml:
[code]
<h1>Admin Layout!</h1>
<%= yield %>
[/code]
我们建立了admin layout,然后在需要使用该layout的controller中指定即可:
[code]
class ProjectsController < ApplicationController
layout "admin"
def index
@projects = Project.find(:all)
end
end
[/code]
[b]4,dynamic layout[/b]
有时候我们需要根据不同的用户角色来使用不同的layout,比如管理员和一般用户,比如博客换肤(也可以用更高级的[url=http://rubyforge.org/projects/theme-generator/]theme-generator[/url])
[code]
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
[/code]
[b]5,action layout[/b]
在action中指定layout即可:
[code]
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
[/code]
上面的index方法指定使用projects layout,当然我们也可以指定不使用layout,如printable页面:
[code]
def index
@projects = Project.find(:all)
render :layout => false
end
[/code]
需要注意的是,这5种layout会按顺序后面的覆盖前面的layout
关于erb和capture的文章:[url]http://hideto.iteye.com/blog/97353[/url]