Skip to content
Newer
Older
100644 63 lines (60 sloc) 2.01 KB
df79e13 @dhh Added first stab at page and fragment caching
dhh authored Jan 8, 2005
1 module ActionView
5a0bc2c @rizwanreza Adds title and description where needed.
rizwanreza authored Jun 16, 2010
2 # = Action View Cache Helper
df79e13 @dhh Added first stab at page and fragment caching
dhh authored Jan 8, 2005
3 module Helpers
4 module CacheHelper
5a0bc2c @rizwanreza Adds title and description where needed.
rizwanreza authored Jun 16, 2010
5 # This helper to exposes a method for caching of view fragments.
6 # See ActionController::Caching::Fragments for usage instructions.
7 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8175
dhh authored Jun 23, 2007
8 # A method for caching fragments of a view rather than an entire
9 # action or page. This technique is useful caching pieces like
10 # menus, lists of news topics, static HTML fragments, and so on.
11 # This method takes a block that contains the content you wish
12 # to cache. See ActionController::Caching::Fragments for more
13 # information.
14 #
15 # ==== Examples
16 # If you wanted to cache a navigation menu, you could do the
17 # following.
18 #
19 # <% cache do %>
20 # <%= render :partial => "menu" %>
21 # <% end %>
22 #
23 # You can also cache static content...
24 #
25 # <% cache do %>
26 # <p>Hello users! Welcome to our website!</p>
27 # <% end %>
28 #
29 # ...and static content mixed with RHTML content.
30 #
31 # <% cache do %>
32 # Topics:
33 # <%= render :partial => "topics", :collection => @topic_list %>
34 # <i>Topics listed alphabetically</i>
35 # <% end %>
2a9ad9c @dhh Moved the caching stores from ActionController::Caching::Fragments::*…
dhh authored Jan 3, 2008
36 def cache(name = {}, options = nil, &block)
585f8f2 @dhh Fixed double output from cache in no caching mode
dhh authored Jun 7, 2010
37 if controller.perform_caching
38 safe_concat(fragment_for(name, options, &block))
39 else
40 yield
41 end
42
c61ed70 Some more tweaks on <% %>.
Carlhuda authored Mar 16, 2010
43 nil
df79e13 @dhh Added first stab at page and fragment caching
dhh authored Jan 8, 2005
44 end
71c9337 All tests pass without memoizing view_context
Carlhuda authored Mar 18, 2010
45
46 private
47 # TODO: Create an object that has caching read/write on it
48 def fragment_for(name = {}, options = nil, &block) #:nodoc:
585f8f2 @dhh Fixed double output from cache in no caching mode
dhh authored Jun 8, 2010
49 if controller.fragment_exist?(name, options)
50 controller.read_fragment(name, options)
71c9337 All tests pass without memoizing view_context
Carlhuda authored Mar 18, 2010
51 else
585f8f2 @dhh Fixed double output from cache in no caching mode
dhh authored Jun 8, 2010
52 # VIEW TODO: Make #capture usable outside of ERB
53 # This dance is needed because Builder can't use capture
54 pos = output_buffer.length
55 yield
56 fragment = output_buffer.slice!(pos..-1)
57 controller.write_fragment(name, fragment, options)
71c9337 All tests pass without memoizing view_context
Carlhuda authored Mar 18, 2010
58 end
59 end
df79e13 @dhh Added first stab at page and fragment caching
dhh authored Jan 8, 2005
60 end
61 end
62 end
Something went wrong with that request. Please try again.