Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 152 lines (148 sloc) 5.586 kb
823a918 David Heinemeier Hansson Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
1 module ActionView
2 module Helpers
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
3 # CaptureHelper exposes methods to let you extract generated markup which
4 # can be used in other parts of a template or layout file.
5 # It provides a method to capture blocks into variables through capture and
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
6 # a way to capture a block of markup for use in a layout through content_for.
823a918 David Heinemeier Hansson Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
7 module CaptureHelper
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
8 # The capture method allows you to extract part of a template into a
9 # variable. You can then use this variable anywhere in your templates or layout.
823a918 David Heinemeier Hansson Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
10 #
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
11 # ==== Examples
80150a0 David Heinemeier Hansson Its just ERb now
dhh authored
12 # The capture method can be used in ERb templates...
823a918 David Heinemeier Hansson Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
13 #
14 # <% @greeting = capture do %>
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
15 # Welcome to my shiny new web page! The date and time is
16 # <%= Time.now %>
3d1b51b David Heinemeier Hansson Added .rxml (and any non-rhtml template, really) supportfor CaptureHelpe...
dhh authored
17 # <% end %>
18 #
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
19 # ...and Builder (RXML) templates.
3d1b51b David Heinemeier Hansson Added .rxml (and any non-rhtml template, really) supportfor CaptureHelpe...
dhh authored
20 #
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
21 # @timestamp = capture do
22 # "The current timestamp is #{Time.now}."
3d1b51b David Heinemeier Hansson Added .rxml (and any non-rhtml template, really) supportfor CaptureHelpe...
dhh authored
23 # end
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
24 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
25 # You can then use that variable anywhere else. For example:
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
26 #
27 # <html>
28 # <head><title><%= @greeting %></title></head>
29 # <body>
30 # <b><%= @greeting %></b>
31 # </body></html>
32 #
f2a021d David Heinemeier Hansson Added option to pass in parameters to CaptureHelper#capture, so you can ...
dhh authored
33 def capture(*args, &block)
72f93b5 Jeremy Kemper Check whether blocks are called from erb using a special __in_erb_templa...
jeremy authored
34 # Return captured buffer in erb.
35 if block_called_from_erb?(block)
fe9d2ad Jeremy Kemper Remove some internal dead code that supported content_for
jeremy authored
36 with_output_buffer { block.call(*args) }
0bdb7d3 Jeremy Kemper Work with @output_buffer instead of _erbout
jeremy authored
37 else
6b9f8ad Michael Koziarski Whitespace
NZKoz authored
38 # Return block result otherwise, but protect buffer also.
72f93b5 Jeremy Kemper Check whether blocks are called from erb using a special __in_erb_templa...
jeremy authored
39 with_output_buffer { return block.call(*args) }
0bdb7d3 Jeremy Kemper Work with @output_buffer instead of _erbout
jeremy authored
40 end
823a918 David Heinemeier Hansson Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
41 end
933697a Jeremy Kemper Try replacing _erbout with @output_buffer
jeremy authored
42
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
43 # Calling content_for stores a block of markup in an identifier for later use.
44 # You can make subsequent calls to the stored content in other templates or the layout
45 # by passing the identifier as an argument to <tt>yield</tt>.
823a918 David Heinemeier Hansson Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
46 #
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
47 # ==== Examples
823a918 David Heinemeier Hansson Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
48 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
49 # <% content_for :not_authorized do %>
50 # alert('You are not authorized to do that!')
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
51 # <% end %>
52 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
53 # You can then use <tt>yield :not_authorized</tt> anywhere in your templates.
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
54 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
55 # <%= yield :not_authorized if current_user.nil? %>
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
56 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
57 # You can also use this syntax alongside an existing call to <tt>yield</tt> in a layout. For example:
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
58 #
e70bb80 David Heinemeier Hansson Stop rdoc from whining
dhh authored
59 # <%# This is the layout %>
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
60 # <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
61 # <head>
62 # <title>My Website</title>
63 # <%= yield :script %>
64 # </head>
65 # <body>
66 # <%= yield %>
67 # </body>
68 # </html>
69 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
70 # And now, we'll create a view that has a content_for call that
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
71 # creates the <tt>script</tt> identifier.
72 #
e70bb80 David Heinemeier Hansson Stop rdoc from whining
dhh authored
73 # <%# This is our view %>
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
74 # Please login!
75 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
76 # <% content_for :script do %>
77 # <script type="text/javascript">alert('You are not authorized to view this page!')</script>
823a918 David Heinemeier Hansson Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
78 # <% end %>
79 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
80 # Then, in another view, you could to do something like this:
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
81 #
82 # <%= link_to_remote 'Logout', :action => 'logout' %>
83 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
84 # <% content_for :script do %>
b00e6a9 David Heinemeier Hansson Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
85 # <%= javascript_include_tag :defaults %>
86 # <% end %>
7bb2065 Update layout and content_for documentation to use yield rather than mag...
Marcel Molina authored
87 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
88 # That will place <script> tags for Prototype, Scriptaculous, and application.js (if it exists)
89 # on the page; this technique is useful if you'll only be using these scripts in a few views.
f2a021d David Heinemeier Hansson Added option to pass in parameters to CaptureHelper#capture, so you can ...
dhh authored
90 #
4156497 David Heinemeier Hansson Fixed CaptureHelper#content_for to work with the optional content parame...
dhh authored
91 # Note that content_for concatenates the blocks it is given for a particular
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
92 # identifier in order. For example:
7bb2065 Update layout and content_for documentation to use yield rather than mag...
Marcel Molina authored
93 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
94 # <% content_for :navigation do %>
95 # <li><%= link_to 'Home', :action => 'index' %></li>
96 # <% end %>
97 #
e70bb80 David Heinemeier Hansson Stop rdoc from whining
dhh authored
98 # <%# Add some other content, or use a different template: %>
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
99 #
100 # <% content_for :navigation do %>
101 # <li><%= link_to 'Login', :action => 'login' %></li>
102 # <% end %>
103 #
104 # Then, in another template or layout, this code would render both links in order:
105 #
106 # <ul><%= yield :navigation %></ul>
4156497 David Heinemeier Hansson Fixed CaptureHelper#content_for to work with the optional content parame...
dhh authored
107 #
108 # Lastly, simple content can be passed as a parameter:
109 #
110 # <% content_for :script, javascript_include_tag(:defaults) %>
111 #
dfc15e1 Jeremy Kemper Improve capture helper documentation. Closes #8796.
jeremy authored
112 # WARNING: content_for is ignored in caches. So you shouldn't use it
113 # for elements that will be fragment cached.
114 #
115 # The deprecated way of accessing a content_for block is to use an instance variable
b7aa223 David Heinemeier Hansson Update doc (closes #11402)
dhh authored
116 # named <tt>@content_for_#{name_of_the_content_block}</tt>. The preferred usage is now
7bb2065 Update layout and content_for documentation to use yield rather than mag...
Marcel Molina authored
117 # <tt><%= yield :footer %></tt>.
afd288c David Heinemeier Hansson Fixed that setting RAILS_ASSET_ID to "" should not add a trailing slash ...
dhh authored
118 def content_for(name, content = nil, &block)
933697a Jeremy Kemper Try replacing _erbout with @output_buffer
jeremy authored
119 ivar = "@content_for_#{name}"
fe9d2ad Jeremy Kemper Remove some internal dead code that supported content_for
jeremy authored
120 content = capture(&block) if block_given?
121 instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{content}")
72f93b5 Jeremy Kemper Check whether blocks are called from erb using a special __in_erb_templa...
jeremy authored
122 nil
823a918 David Heinemeier Hansson Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
123 end
3d1b51b David Heinemeier Hansson Added .rxml (and any non-rhtml template, really) supportfor CaptureHelpe...
dhh authored
124
ecc0543 Jeremy Kemper JavaScriptGenerator should only sets output_buffer for the duration of t...
jeremy authored
125 # Use an alternate output buffer for the duration of the block.
126 # Defaults to a new empty string.
e23554d Jeremy Kemper Ruby 1.9: flushing the output buffer preserves its encoding
jeremy authored
127 def with_output_buffer(buf = nil) #:nodoc:
128 unless buf
129 buf = ''
130 buf.force_encoding(output_buffer.encoding) if buf.respond_to?(:force_encoding)
131 end
ecc0543 Jeremy Kemper JavaScriptGenerator should only sets output_buffer for the duration of t...
jeremy authored
132 self.output_buffer, old_buffer = buf, output_buffer
133 yield
134 output_buffer
135 ensure
136 self.output_buffer = old_buffer
137 end
3d26076 Jeremy Kemper Introduce flush_output_buffer to append the buffer to the response body ...
jeremy authored
138
139 # Add the output buffer to the response body and start a new one.
140 def flush_output_buffer #:nodoc:
e23554d Jeremy Kemper Ruby 1.9: flushing the output buffer preserves its encoding
jeremy authored
141 if output_buffer && !output_buffer.empty?
3d26076 Jeremy Kemper Introduce flush_output_buffer to append the buffer to the response body ...
jeremy authored
142 response.body_parts << output_buffer
e23554d Jeremy Kemper Ruby 1.9: flushing the output buffer preserves its encoding
jeremy authored
143 new = ''
144 new.force_encoding(output_buffer.encoding) if new.respond_to?(:force_encoding)
145 self.output_buffer = new
146 nil
3d26076 Jeremy Kemper Introduce flush_output_buffer to append the buffer to the response body ...
jeremy authored
147 end
148 end
823a918 David Heinemeier Hansson Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
149 end
150 end
151 end
Something went wrong with that request. Please try again.