诗歌rails之layout

一般来说layout有如下五种:
gobal layout,controller layout,shared layout,dynamic layout,action layout
dynamic layout
有时候我们需要根据不同的用户角色来使用不同的layout,比如管理员和一般用户,比如博客换肤(也可以用更高级的theme-generator)
ruby代码
  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  
action layout
在action中指定layout即可:
ruby代码
  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  
关键字: Rails layout content_for 如果我们想根据模板页面更改局部layout,使用content_for即可。
content_for允许模板页面代码放到layout中的任何位置。

比如我们的Rails程序不同的页面有不同的css样式,我们可以在layout里留出位置:
ruby代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
  2.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
  3. <html>  
  4.   <head>  
  5.     <title>Todo List</title>  
  6.     <%= stylesheet_link_tag 'application' %>  
  7.     <%= yield :head %>  
  8.   </head>  
  9.   <body>  
  10.     <div id="container">  
  11.       <h1>Todo List</h1>  
  12.       <%= yield %>  
  13.     </div>  
  14.   </body>  
  15. </html>  

我们用yield :head来给模板页面某段代码留个"座位",再看页面:
ruby代码
  1. <% content_for :head do %>  
  2.   <%= stylesheet_link_tag 'projects' %>  
  3. <% end %>  
  4. <h2>Projects</h2>  
  5. <ul>  
  6. <% for project in @projects %>  
  7.   <li><%= project.name %></li>  
  8. <% end %>  

content_for :head里面的代码将填充layout里的yield :head。

关键字:使用content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)

Examples

 content_tag(:p, "Hello world!")
# => <p>Hello world!</p>
content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
# => <div class="strong"><p>Hello world!</p></div>
content_tag("select", options, :multiple => true)
# => <select multiple="multiple">...options...</select>
<% content_tag :div, :class => "strong" do -%>
Hello world!
<% end -%>
# => <div class="strong">Hello world!</div>
 # File vendor/rails/actionpack/lib/action_view/helpers/tag_helper.rb, line 67
67: def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
68: if block_given?
69: options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
70: content_tag = content_tag_string(name, capture(&block), options, escape)
71:
72: if block_called_from_erb?(block)
73: concat(content_tag)
74: else
75: content_tag
76: end
77: else
78: content_tag_string(name, content_or_options_with_block, options, escape)
79: end
80: end


posted @ 2009-07-07 13:46  麦飞  阅读(1285)  评论(0编辑  收藏  举报