lib/roda/plugins/render.rb in roda-0.9.0 vs lib/roda/plugins/render.rb in roda-1.0.0

- old
+ new

@@ -25,14 +25,15 @@ # # render_opts[:views] = 'admin_views' # # The following options are supported: # - # :cache :: A specific cache to store templates in, or nil/false to not - # cache templates (useful for development), defaults to true to - # automatically use the default template cache. + # :cache :: nil/false to not cache templates (useful for development), defaults + # to true to automatically use the default template cache. # :engine :: The tilt engine to use for rendering, defaults to 'erb'. + # :escape :: Use Roda's Erubis escaping support, which handles postfix + # conditions inside <%= %> tags. # :ext :: The file extension to assume for view files, defaults to the :engine # option. # :layout :: The base name of the layout file, defaults to 'layout'. # :layout_opts :: The options to use when rendering the layout, if different # from the default options. @@ -48,10 +49,13 @@ # render('foo', :views=>'admin_views') # # There are a couple of additional options to +view+ and +render+ that are # available at runtime: # + # :content :: Only respected by +view+, provides the content to render + # inside the layout, instead of rendering a template to get + # the content. # :inline :: Use the value given as the template code, instead of looking # for template code in a file. # :locals :: Hash of local variables to make available inside the template. # :path :: Use the value given as the full pathname for the file, instead # of using the :views and :ext option in combination with the @@ -63,36 +67,14 @@ # render(:path=>'/path/to/template.erb') # # If you pass a hash as the first argument to +view+ or +render+, it should # have either +:inline+ or +:path+ as one of the keys. module Render - # Default template cache. Thread-safe so that multiple threads can - # simultaneously use the cache. - class Cache - # Mutex used to synchronize access to the cache. Uses a - # singleton mutex to reduce memory. - MUTEX = ::Mutex.new - - # Initialize the cache. - def initialize - MUTEX.synchronize{@cache = {}} + def self.load_dependencies(app, opts={}) + if opts[:escape] + app.plugin :_erubis_escaping end - - # Clear the cache. - alias clear initialize - - # If the template is found in the cache under the given key, - # return it, otherwise yield to get the template, and - # store the template under the given key - def fetch(key) - unless template = MUTEX.synchronize{@cache[key]} - template = yield - MUTEX.synchronize{@cache[key] = template} - end - - template - end end # Setup default rendering options. See Render for details. def self.configure(app, opts={}) if app.opts[:render] @@ -110,12 +92,14 @@ opts[:opts] ||= (opts[:opts] || {}).dup opts[:opts][:outvar] ||= '@_out_buf' if RUBY_VERSION >= "1.9" opts[:opts][:default_encoding] ||= Encoding.default_external end - cache = opts.fetch(:cache, true) - opts[:cache] = Cache.new if cache == true + if opts[:escape] + opts[:opts][:engine_class] = ErubisEscaping::Eruby + end + opts[:cache] = app.thread_safe_cache if opts.fetch(:cache, true) end module ClassMethods # Copy the rendering options into the subclass, duping # them as necessary to prevent changes in the subclass @@ -123,11 +107,11 @@ def inherited(subclass) super opts = subclass.opts[:render] = render_opts.dup opts[:layout_opts] = opts[:layout_opts].dup opts[:opts] = opts[:opts].dup - opts[:cache] = Cache.new if opts[:cache] + opts[:cache] = thread_safe_cache if opts[:cache] end # Return the render options for this class. def render_opts opts[:render] @@ -177,11 +161,11 @@ else opts = opts.merge(template) end end - content = render(template, opts) + content = opts[:content] || render(template, opts) if layout = opts.fetch(:layout, render_opts[:layout]) if layout_opts = opts[:layout_opts] layout_opts = render_opts[:layout_opts].merge(layout_opts) end @@ -196,10 +180,13 @@ # If caching templates, attempt to retrieve the template from the cache. Otherwise, just yield # to get the template. def cached_template(path, &block) if cache = render_opts[:cache] - cache.fetch(path, &block) + unless template = cache[path] + template = cache[path] = yield + end + template else yield end end