Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 145 lines (141 sloc) 5.321 kb
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#cont…
dhh authored
1 module ActionView
2 module Helpers
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
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 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 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#cont…
dhh authored
7 module CaptureHelper
dfc15e1 @jeremy 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 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#cont…
dhh authored
10 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
11 # ==== Examples
80150a0 @dhh Its just ERb now
dhh authored
12 # The capture method can be used in ERb templates...
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#cont…
dhh authored
13 #
14 # <% @greeting = capture do %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
15 # Welcome to my shiny new web page! The date and time is
16 # <%= Time.now %>
3d1b51b @dhh Added .rxml (and any non-rhtml template, really) supportfor CaptureHe…
dhh authored
17 # <% end %>
18 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
19 # ...and Builder (RXML) templates.
3d1b51b @dhh Added .rxml (and any non-rhtml template, really) supportfor CaptureHe…
dhh authored
20 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
21 # @timestamp = capture do
22 # "The current timestamp is #{Time.now}."
3d1b51b @dhh Added .rxml (and any non-rhtml template, really) supportfor CaptureHe…
dhh authored
23 # end
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
24 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
25 # You can then use that variable anywhere else. For example:
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
26 #
27 # <html>
28 # <head><title><%= @greeting %></title></head>
29 # <body>
30 # <b><%= @greeting %></b>
31 # </body></html>
32 #
f2a021d @dhh Added option to pass in parameters to CaptureHelper#capture, so you c…
dhh authored
33 def capture(*args, &block)
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
34 # Return captured buffer in erb.
35 if block_called_from_erb?(block)
fe9d2ad @jeremy Remove some internal dead code that supported content_for
jeremy authored
36 with_output_buffer { block.call(*args) }
0bdb7d3 @jeremy Work with @output_buffer instead of _erbout
jeremy authored
37 else
6b9f8ad @NZKoz Whitespace
NZKoz authored
38 # Return block result otherwise, but protect buffer also.
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
39 with_output_buffer { return block.call(*args) }
0bdb7d3 @jeremy Work with @output_buffer instead of _erbout
jeremy authored
40 end
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#cont…
dhh authored
41 end
933697a @jeremy Try replacing _erbout with @output_buffer
jeremy authored
42
dfc15e1 @jeremy 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 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#cont…
dhh authored
46 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
47 # ==== Examples
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#cont…
dhh authored
48 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
49 # <% content_for :not_authorized do %>
50 # alert('You are not authorized to do that!')
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
51 # <% end %>
52 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
53 # You can then use <tt>yield :not_authorized</tt> anywhere in your templates.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
54 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
55 # <%= yield :not_authorized if current_user.nil? %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
56 #
dfc15e1 @jeremy 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 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
58 #
e70bb80 @dhh Stop rdoc from whining
dhh authored
59 # <%# This is the layout %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
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 Improve capture helper documentation. Closes #8796.
jeremy authored
70 # And now, we'll create a view that has a content_for call that
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
71 # creates the <tt>script</tt> identifier.
72 #
e70bb80 @dhh Stop rdoc from whining
dhh authored
73 # <%# This is our view %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
74 # Please login!
75 #
dfc15e1 @jeremy 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 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#cont…
dhh authored
78 # <% end %>
79 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
80 # Then, in another view, you could to do something like this:
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
81 #
82 # <%= link_to_remote 'Logout', :action => 'logout' %>
83 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
84 # <% content_for :script do %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
85 # <%= javascript_include_tag :defaults %>
86 # <% end %>
7bb2065 Update layout and content_for documentation to use yield rather than …
Marcel Molina authored
87 #
dfc15e1 @jeremy 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 @dhh Added option to pass in parameters to CaptureHelper#capture, so you c…
dhh authored
90 #
4156497 @dhh Fixed CaptureHelper#content_for to work with the optional content par…
dhh authored
91 # Note that content_for concatenates the blocks it is given for a particular
dfc15e1 @jeremy 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 …
Marcel Molina authored
93 #
dfc15e1 @jeremy 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 @dhh Stop rdoc from whining
dhh authored
98 # <%# Add some other content, or use a different template: %>
dfc15e1 @jeremy 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 @dhh Fixed CaptureHelper#content_for to work with the optional content par…
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 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 @dhh 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 …
Marcel Molina authored
117 # <tt><%= yield :footer %></tt>.
afd288c @dhh Fixed that setting RAILS_ASSET_ID to "" should not add a trailing sla…
dhh authored
118 def content_for(name, content = nil, &block)
933697a @jeremy Try replacing _erbout with @output_buffer
jeremy authored
119 ivar = "@content_for_#{name}"
fe9d2ad @jeremy 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 Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
122 nil
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#cont…
dhh authored
123 end
3d1b51b @dhh Added .rxml (and any non-rhtml template, really) supportfor CaptureHe…
dhh authored
124
ecc0543 @jeremy JavaScriptGenerator should only sets output_buffer for the duration o…
jeremy authored
125 # Use an alternate output buffer for the duration of the block.
126 # Defaults to a new empty string.
127 def with_output_buffer(buf = '') #:nodoc:
128 self.output_buffer, old_buffer = buf, output_buffer
129 yield
130 output_buffer
131 ensure
132 self.output_buffer = old_buffer
133 end
3d26076 @jeremy Introduce flush_output_buffer to append the buffer to the response bo…
jeremy authored
134
135 # Add the output buffer to the response body and start a new one.
136 def flush_output_buffer #:nodoc:
137 if output_buffer && output_buffer != ''
138 response.body_parts << output_buffer
139 self.output_buffer = ''
140 end
141 end
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#cont…
dhh authored
142 end
143 end
144 end
Something went wrong with that request. Please try again.