root/lang/ruby/pritter/vendor/rails/actionpack/lib/action_view/helpers/capture_helper.rb @ 5644

Revision 5644, 3.8 kB (checked in by nakamud, 7 years ago)

lang/ruby/pritter: freezed gems to ease installation

RevLine 
[5644]1module ActionView
2  module Helpers
3    # Capture lets you extract parts of code which
4    # can be used in other points of the template or even layout file.
5    #
6    # == Capturing a block into an instance variable
7    #
8    #   <% @script = capture do %>
9    #     [some html...]
10    #   <% end %>
11    #
12    # == Add javascript to header using content_for
13    #
14    # content_for("name") is a wrapper for capture which will
15    # make the fragment available by name to a yielding layout or template.
16    #
17    # layout.rhtml:
18    #
19    #   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
20    #   <head>
21    #       <title>layout with js</title>
22    #       <script type="text/javascript">
23    #         <%= yield :script %>
24    #     </script>
25    #   </head>
26    #   <body>
27    #     <%= yield %>
28    #   </body>
29    #   </html>
30    #
31    # view.rhtml
32    #   
33    #   This page shows an alert box!
34    #
35    #   <% content_for("script") do %>
36    #     alert('hello world')
37    #   <% end %>
38    #
39    #   Normal view text
40    module CaptureHelper
41      # Capture allows you to extract a part of the template into an
42      # instance variable. You can use this instance variable anywhere
43      # in your templates and even in your layout.
44      #
45      # Example of capture being used in a .rhtml page:
46      #
47      #   <% @greeting = capture do %>
48      #     Welcome To my shiny new web page!
49      #   <% end %>
50      #
51      # Example of capture being used in a .rxml page:
52      #
53      #   @greeting = capture do
54      #     'Welcome To my shiny new web page!'
55      #   end
56      def capture(*args, &block)
57        # execute the block
58        begin
59          buffer = eval("_erbout", block.binding)
60        rescue
61          buffer = nil
62        end
63       
64        if buffer.nil?
65          capture_block(*args, &block)
66        else
67          capture_erb_with_buffer(buffer, *args, &block)
68        end
69      end
70     
71      # Calling content_for stores the block of markup for later use.
72      # Subsequently, you can make calls to it by name with <tt>yield</tt>
73      # in another template or in the layout.
74      #
75      # Example:
76      #
77      #   <% content_for("header") do %>
78      #     alert('hello world')
79      #   <% end %>
80      #
81      # You can use yield :header anywhere in your templates.
82      #
83      #   <%= yield :header %>
84      #
85      # NOTE: Beware that content_for is ignored in caches. So you shouldn't use it
86      # for elements that are going to be fragment cached.
87      #
88      # The deprecated way of accessing a content_for block was to use a instance variable
89      # named @@content_for_#{name_of_the_content_block}@. So <tt><%= content_for('footer') %></tt>
90      # would be avaiable as <tt><%= @content_for_footer %></tt>. The preferred notation now is
91      # <tt><%= yield :footer %></tt>.
92      def content_for(name, content = nil, &block)
93        eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)"
94      end
95
96      private
97        def capture_block(*args, &block)
98          block.call(*args)
99        end
100     
101        def capture_erb(*args, &block)
102          buffer = eval("_erbout", block.binding)
103          capture_erb_with_buffer(buffer, *args, &block)
104        end
105     
106        def capture_erb_with_buffer(buffer, *args, &block)
107          pos = buffer.length
108          block.call(*args)
109       
110          # extract the block
111          data = buffer[pos..-1]
112       
113          # replace it in the original with empty string
114          buffer[pos..-1] = ''
115       
116          data
117        end
118     
119        def erb_content_for(name, &block)
120          eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_erb(&block)"
121        end
122     
123        def block_content_for(name, &block)
124          eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_block(&block)"
125        end
126    end
127  end
128end
Note: See TracBrowser for help on using the browser.