Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 204 lines (197 sloc) 7.407 kb
76f024a @fxn adds missing requires for Object#blank? and Object#present?
fxn authored
1 require 'active_support/core_ext/object/blank'
bb9c58e @jeffkreeftmeijer Make sure capture's output gets html_escaped [#5545 state:resolved]
jeffkreeftmeijer authored
2 require 'active_support/core_ext/string/output_safety'
76f024a @fxn adds missing requires for Object#blank? and Object#present?
fxn authored
3
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
4 module ActionView
5a0bc2c @rizwanreza Adds title and description where needed.
rizwanreza authored
5 # = Action View Capture Helper
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
6 module Helpers
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
7 # CaptureHelper exposes methods to let you extract generated markup which
8 # can be used in other parts of a template or layout file.
5a0bc2c @rizwanreza Adds title and description where needed.
rizwanreza authored
9 #
9de8305 Add deprecation notices for <% %>.
Carlhuda authored
10 # It provides a method to capture blocks into variables through capture and
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
11 # 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#content...
dhh authored
12 module CaptureHelper
9de8305 Add deprecation notices for <% %>.
Carlhuda authored
13 # The capture method allows you to extract part of a template into a
14 # variable. You can then use this variable anywhere in your templates or layout.
15 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
16 # ==== Examples
bd3cdee @amatsuda s/ERb/ERB/g
amatsuda authored
17 # The capture method can be used in ERB templates...
9de8305 Add deprecation notices for <% %>.
Carlhuda authored
18 #
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
19 # <% @greeting = capture do %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
20 # Welcome to my shiny new web page! The date and time is
21 # <%= Time.now %>
3d1b51b @dhh Added .rxml (and any non-rhtml template, really) supportfor CaptureHelpe...
dhh authored
22 # <% end %>
23 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
24 # ...and Builder (RXML) templates.
9de8305 Add deprecation notices for <% %>.
Carlhuda authored
25 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
26 # @timestamp = capture do
27 # "The current timestamp is #{Time.now}."
3d1b51b @dhh Added .rxml (and any non-rhtml template, really) supportfor CaptureHelpe...
dhh authored
28 # end
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
29 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
30 # You can then use that variable anywhere else. For example:
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
31 #
32 # <html>
33 # <head><title><%= @greeting %></title></head>
34 # <body>
35 # <b><%= @greeting %></b>
36 # </body></html>
37 #
4464b8e Deprecate block_called_from_erb? pending a solution for getting it into ...
Carlhuda authored
38 def capture(*args)
39 value = nil
cd9ffd1 @wycats Eliminate warnings for AM on 1.8
wycats authored
40 buffer = with_output_buffer { value = yield(*args) }
9de8305 Add deprecation notices for <% %>.
Carlhuda authored
41 if string = buffer.presence || value and string.is_a?(String)
72d12d9 @spastorino Call as ERB::Util.html_escape since is not the module is not included he...
spastorino authored
42 ERB::Util.html_escape string
9de8305 Add deprecation notices for <% %>.
Carlhuda authored
43 end
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
44 end
933697a @jeremy Try replacing _erbout with @output_buffer
jeremy authored
45
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
46 # Calling content_for stores a block of markup in an identifier for later use.
f45714b @jeroenvandijk Added extra documentation for content_for helper
jeroenvandijk authored
47 # You can make subsequent calls to the stored content in other templates, helper modules
48 # or the layout by passing the identifier as an argument to <tt>content_for</tt>.
49 #
50 # Note: <tt>yield</tt> can still be used to retrieve the stored content, but calling
51 # <tt>yield</tt> doesn't work in helper modules, while <tt>content_for</tt> does.
9de8305 Add deprecation notices for <% %>.
Carlhuda authored
52 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
53 # ==== Examples
9de8305 Add deprecation notices for <% %>.
Carlhuda authored
54 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
55 # <% content_for :not_authorized do %>
56 # alert('You are not authorized to do that!')
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
57 # <% end %>
58 #
f45714b @jeroenvandijk Added extra documentation for content_for helper
jeroenvandijk authored
59 # You can then use <tt>content_for :not_authorized</tt> anywhere in your templates.
60 #
61 # <%= content_for :not_authorized if current_user.nil? %>
62 #
63 # This is equivalent to:
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
64 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
65 # <%= yield :not_authorized if current_user.nil? %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
66 #
f45714b @jeroenvandijk Added extra documentation for content_for helper
jeroenvandijk authored
67 # <tt>content_for</tt>, however, can also be used in helper modules.
68 #
69 # module StorageHelper
70 # def stored_content
71 # content_for(:storage) || "Your storage is empty"
72 # end
73 # end
74 #
75 # This helper works just like normal helpers.
76 #
77 # <%= stored_content %>
78 #
79 # You can use the <tt>yield</tt> 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, #8175...
dhh authored
80 #
e70bb80 @dhh Stop rdoc from whining
dhh authored
81 # <%# This is the layout %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
82 # <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
83 # <head>
84 # <title>My Website</title>
85 # <%= yield :script %>
86 # </head>
87 # <body>
88 # <%= yield %>
89 # </body>
90 # </html>
91 #
f45714b @jeroenvandijk Added extra documentation for content_for helper
jeroenvandijk authored
92 # And now, we'll create a view that has a <tt>content_for</tt> call that
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
93 # creates the <tt>script</tt> identifier.
94 #
e70bb80 @dhh Stop rdoc from whining
dhh authored
95 # <%# This is our view %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
96 # Please login!
97 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
98 # <% content_for :script do %>
99 # <script type="text/javascript">alert('You are not authorized to view this page!')</script>
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
100 # <% end %>
101 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
102 # Then, in another view, you could to do something like this:
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
103 #
9e1e95f @jeremy link_to_remote -> link_to :remote => true
jeremy authored
104 # <%= link_to 'Logout', :action => 'logout', :remote => true %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
105 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
106 # <% content_for :script do %>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175...
dhh authored
107 # <%= javascript_include_tag :defaults %>
108 # <% end %>
7bb2065 Update layout and content_for documentation to use yield rather than mag...
Marcel Molina authored
109 #
06bdaae @fxn removes ActionView::Helpers::ScriptaculousHelper
fxn authored
110 # That will place +script+ tags for your default set of JavaScript files on the page;
111 # 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 can ...
dhh authored
112 #
4156497 @dhh Fixed CaptureHelper#content_for to work with the optional content parame...
dhh authored
113 # Note that content_for concatenates the blocks it is given for a particular
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
114 # identifier in order. For example:
7bb2065 Update layout and content_for documentation to use yield rather than mag...
Marcel Molina authored
115 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
116 # <% content_for :navigation do %>
117 # <li><%= link_to 'Home', :action => 'index' %></li>
118 # <% end %>
119 #
e70bb80 @dhh Stop rdoc from whining
dhh authored
120 # <%# Add some other content, or use a different template: %>
9de8305 Add deprecation notices for <% %>.
Carlhuda authored
121 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
122 # <% content_for :navigation do %>
123 # <li><%= link_to 'Login', :action => 'login' %></li>
124 # <% end %>
125 #
126 # Then, in another template or layout, this code would render both links in order:
127 #
f45714b @jeroenvandijk Added extra documentation for content_for helper
jeroenvandijk authored
128 # <ul><%= content_for :navigation %></ul>
4156497 @dhh Fixed CaptureHelper#content_for to work with the optional content parame...
dhh authored
129 #
130 # Lastly, simple content can be passed as a parameter:
131 #
132 # <% content_for :script, javascript_include_tag(:defaults) %>
133 #
dfc15e1 @jeremy Improve capture helper documentation. Closes #8796.
jeremy authored
134 # WARNING: content_for is ignored in caches. So you shouldn't use it
135 # for elements that will be fragment cached.
afd288c @dhh Fixed that setting RAILS_ASSET_ID to "" should not add a trailing slash ...
dhh authored
136 def content_for(name, content = nil, &block)
fe9d2ad @jeremy Remove some internal dead code that supported content_for
jeremy authored
137 content = capture(&block) if block_given?
86a0f7f @josevalim Add tests for content_for() for read, closes #475.
josevalim authored
138 if content
139 @view_flow.append(name, content)
140 nil
141 else
142 @view_flow.get(name)
143 end
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
144 end
3d1b51b @dhh Added .rxml (and any non-rhtml template, really) supportfor CaptureHelpe...
dhh authored
145
e30ca00 @josevalim Yo dawg, I heard you like streaming. So I put a fiber, inside a block, i...
josevalim authored
146 # The same as +content_for+ but when used with streaming flushes
147 # straight back to the layout. In other words, if you want to
148 # concatenate several times to the same buffer when rendering a given
ab105e6 @josevalim content_for should work with provide.
josevalim authored
149 # template, you should use +content_for+, if not, use +provide+ to tell
150 # the layout to stop looking for more contents.
e30ca00 @josevalim Yo dawg, I heard you like streaming. So I put a fiber, inside a block, i...
josevalim authored
151 def provide(name, content = nil, &block)
152 content = capture(&block) if block_given?
33cc001 @josevalim More cleanup and moving responsibilities around.
josevalim authored
153 result = @view_flow.append!(name, content) if content
ab105e6 @josevalim content_for should work with provide.
josevalim authored
154 result unless content
e30ca00 @josevalim Yo dawg, I heard you like streaming. So I put a fiber, inside a block, i...
josevalim authored
155 end
156
9cb8c81 @darragh Add content_for?(:name) helper to check if content_for(:name) is present...
darragh authored
157 # content_for? simply checks whether any content has been captured yet using content_for
158 # Useful to render parts of your layout differently based on what is in your views.
9de8305 Add deprecation notices for <% %>.
Carlhuda authored
159 #
9cb8c81 @darragh Add content_for?(:name) helper to check if content_for(:name) is present...
darragh authored
160 # ==== Examples
161 #
162 # Perhaps you will use different css in you layout if no content_for :right_column
163 #
164 # <%# This is the layout %>
165 # <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
166 # <head>
167 # <title>My Website</title>
168 # <%= yield :script %>
169 # </head>
170 # <body class="<%= content_for?(:right_col) ? 'one-column' : 'two-column' %>">
171 # <%= yield %>
172 # <%= yield :right_col %>
173 # </body>
174 # </html>
175 def content_for?(name)
33cc001 @josevalim More cleanup and moving responsibilities around.
josevalim authored
176 @view_flow.get(name).present?
9cb8c81 @darragh Add content_for?(:name) helper to check if content_for(:name) is present...
darragh authored
177 end
178
ecc0543 @jeremy JavaScriptGenerator should only sets output_buffer for the duration of t...
jeremy authored
179 # Use an alternate output buffer for the duration of the block.
180 # Defaults to a new empty string.
e23554d @jeremy Ruby 1.9: flushing the output buffer preserves its encoding
jeremy authored
181 def with_output_buffer(buf = nil) #:nodoc:
182 unless buf
4464b8e Deprecate block_called_from_erb? pending a solution for getting it into ...
Carlhuda authored
183 buf = ActionView::OutputBuffer.new
8d5b792 @sespindola Fixed output_buffer encoding problem [#5179]
sespindola authored
184 buf.force_encoding(output_buffer.encoding) if output_buffer.respond_to?(:encoding) && buf.respond_to?(:force_encoding)
e23554d @jeremy Ruby 1.9: flushing the output buffer preserves its encoding
jeremy authored
185 end
ecc0543 @jeremy JavaScriptGenerator should only sets output_buffer for the duration of t...
jeremy authored
186 self.output_buffer, old_buffer = buf, output_buffer
187 yield
188 output_buffer
189 ensure
190 self.output_buffer = old_buffer
191 end
3d26076 @jeremy Introduce flush_output_buffer to append the buffer to the response body ...
jeremy authored
192
193 # Add the output buffer to the response body and start a new one.
194 def flush_output_buffer #:nodoc:
e23554d @jeremy Ruby 1.9: flushing the output buffer preserves its encoding
jeremy authored
195 if output_buffer && !output_buffer.empty?
3d26076 @jeremy Introduce flush_output_buffer to append the buffer to the response body ...
jeremy authored
196 response.body_parts << output_buffer
b3318bd @jeremy Slice new buffer from beginning of old buffer to skip encoding special c...
jeremy authored
197 self.output_buffer = output_buffer[0,0]
e23554d @jeremy Ruby 1.9: flushing the output buffer preserves its encoding
jeremy authored
198 nil
3d26076 @jeremy Introduce flush_output_buffer to append the buffer to the response body ...
jeremy authored
199 end
200 end
823a918 @dhh Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content...
dhh authored
201 end
202 end
203 end
Something went wrong with that request. Please try again.