诗歌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  
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 
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  
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 
关键字: 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>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Todo List</title> <%= stylesheet_link_tag 'application' %> <%= yield :head %> </head> <body> <div id="container"> <h1>Todo List</h1> <%= yield %> </div> </body> </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 do %> <%= stylesheet_link_tag 'projects' %> <% end %> <h2>Projects</h2> <ul> <% for project in @projects %> <li><%= project.name %></li> <% 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


转载于:https://www.cnblogs.com/orez88/articles/1518357.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值