ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2008年12月17日 星期三
Block in views, concat, capture, content_for,yield,content_tag
rails 2.2 block in view
block in view before
# 2.2 之前, 叫concat 時都要, 叫block.binding
# 2.2 之後就不用囉
=begin
用這樣就可以不用在view 裡面做一堆 <% if admin 的動作 %>
也可以把view content 不同的部份當成 variable 來用帶入相同的外圍, DRY
=end
#version rails 2.0.2
def admin_area(&block)
if admin?
concat content_tag(:div, capture(&block), :class => 'admin'), block.binding
end
end
#version rails 2.2
def admin_area(&block)
if admin?
concat content_tag(:div, capture(&block), :class => 'admin')
end
end
concat source code
#actionpack-2.0.2/lib/action_view/helpers/text_helper.rb
def concat(string, binding)
eval(ActionView::Base.erb_variable, binding) << string
end
#actionpack-2.2.2/lib/action_view/helpers/text_helper.rb
def concat(string, unused_binding = nil)
if unused_binding
ActiveSupport::Deprecation.warn("The binding argument of #concat is no longer needed. Please remove it from your views and helpers.", caller)
end
output_buffer << string
end
其他學到的 concat, capture, yield,content_tag
concat
# The preferred method of outputting text in your views is to use the
# <%= "text" %> eRuby syntax. The regular _puts_ and _print_ methods
# do not operate as expected in an eRuby code block. If you absolutely must
# output text within a non-output code block (i.e., <% %>), you can use the concat method.
你不想在外面的view, 弄一堆 <% %> 的 tag
可以選擇用一個大的包起來, 然後用concat 把需要呈現在view 的地方印出來
capture
#The capture method allows you to extract part of a template into a variable. You can then use this variable anywhere in your templates or layout.
#可以讓你把把view 存起來當變數, 做一些判斷
#在上面block in view 的作用就是這樣 先存起來讓concat 去用
#如果是用yield 就會直接出去在view了
#views/list.rhtml
<% @header = capture do %>
<%= link_to "#{@user.name}的早起紀錄", home_url, :class => 'ebhead'%>
<% end %>
#layout.rhtml
<% if @header %>
#OOXX
<%= @header %>
#OOXX
<% else %>
#OOXX
<%= @header %>
#OOXX
<% end %>
yield,
content_for
#yield 最常用的到當然就是在layout 叫content
<%= yield %>
#再來就是利用content_for 把 部份的 view 存成block
#layouts/application.html.erb
(head)
<%= yield :head_specific %>
(/head)
#views/show.html.erb
<% content_for :head_specific do %>
#可以根據不同的頁面去放不同的東西例如include 不同的css
#然後等 yield, 這邊就是會在layout 裡 head tag 裡面放入
[%= stylesheet_link_tag "main" %]
[%= javascript_include_tag "spotlight" %]
<% end %>
content_tag
=begin
Returns an HTML block tag of type name surrounding the content. Add HTML attributes by
passing an attributes hash to options. Instead of passing the content as an argument, you
can also use a block in which case, you pass your options as the second parameter. Set
escape to false to disable attribute value escaping.
完全就是很high 的讓你不用寫html 包來包去
尤其是在上面用concat 的地方 不用 concat '
' ... concat '
'
=end
content_tag(:p, "Hello world!")
# =>
Hello world!
content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
# =>
Hello world!
content_tag("select", options, :multiple => true)
# =>
...options...
<% content_tag :div, :class => "strong" do -%>
Hello world!
<% end -%>
# =>
Hello world!
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言