rails view层的具体细节(三)

转自:http://ihower.tw/rails3/actionview.html

Layout版型

Layout可以用来包裹Template样板,让不同View可以共用Layout作为文件的头尾。因此我们可以为全站的页面建立共用的版型。这个档案预设是app/views/layouts/application.html.erb如果在app/views/layouts目录下有跟某Controller同名的Layout档案,那这个Controller下的所有Views就会使用这个同名的Layout

预设的Layout长得如下:

<!DOCTYPE html>
<html>
<head>
  <title>YourApplicationName</title>
  <%= stylesheet_link_tag "application" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

其中的<%= yield %>会被替换成个别Action的样板。

开头的<!DOCTYPE html>说明了这是一份HTML5文件,这种宣告法向下相容于所有浏览器的HTML4

如果需要指定ControllerLayout,可以这么做:

class EventsController < ApplicationController
   layout "special"
end

这样就会指定Events Controller下的Views都使用app/views/layouts/special.html.erb这个Layout,你可以加上参数:only:except表示只有特定的Action

class EventsController < ApplicationController
   layout "special", :only => :index
end

或是

class EventsController < ApplicationController
   layout "special", :except => [:show, :edit, :new]
end

请注意到使用字串和Symbol是不同的。使用Symbol的话,它会透过一个同名的方法来动态决定,例如以下的Layout是透过determine_layout这个方法来决定:

class EventsController < ApplicationController
   layout :determine_layout

	private

	def determine_layout
   	   ( rand(100)%2 == 0 )? "event_open" : "event_closed"
	end
end

除了 ​​在Controller层级设定Layout,我们也可以设定个别的Action使用不同的Layout,例如:

def show
   @event = Event.find(params[:id])
	render :layout => "foobar"
end

这样show Action的样板就会套用foobar Layout更常见的情形是关掉Layout,这时候我们可以写render :layout => false

自定Layout内容

除了​​<%= yield %>会载入Template内容之外,我们也可以预先自定一些其他的区块让Template可以置入内容。例如,要在Layout中放一个侧栏用的区块,取名叫做:sidebar

<div id="sidebar">
    <%= yield :sidebar %>
</div>
<div id="content">
    <%= yield %>
</div>

那么在Template样板中,任意地方放:

<%= content_for :sidebar do %>
   <ul>
       <li>foo</li>
       <li>bar</li>
   </ul>
<% end %>

那么这段内容就会被置入到Layout<%= yield :sidebar %>之中。

Layout版型的继承(进阶)


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值