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

- old
+ new

@@ -16,24 +16,22 @@ # r.is 'bar' do # render('bar') # renders views/bar.erb # end # end # - # You can provide options to the plugin method, or later by modifying - # +render_opts+. + # You can provide options to the plugin method: # - # plugin :render, :engine=>'haml' + # plugin :render, :engine=>'haml', :views=>'admin_views' # - # render_opts[:views] = 'admin_views' - # # The following options are supported: # # :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. + # :escape :: Use Roda's Erubis escaping support, which makes <%= %> escape output, + # <%== %> not escape output, and 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. @@ -67,27 +65,29 @@ # 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 - def self.load_dependencies(app, opts={}) + OPTS={}.freeze + + def self.load_dependencies(app, opts=OPTS) if opts[:escape] app.plugin :_erubis_escaping end end # Setup default rendering options. See Render for details. - def self.configure(app, opts={}) + def self.configure(app, opts=OPTS) if app.opts[:render] app.opts[:render].merge!(opts) - else + else app.opts[:render] = opts.dup end opts = app.opts[:render] opts[:engine] ||= "erb" - opts[:ext] = nil unless opts.has_key?(:ext) + opts[:ext] = nil unless opts.has_key?(:ext) opts[:views] ||= File.expand_path("views", Dir.pwd) opts[:layout] = "layout" unless opts.has_key?(:layout) opts[:layout_opts] ||= (opts[:layout_opts] || {}).dup opts[:opts] ||= (opts[:opts] || {}).dup opts[:opts][:outvar] ||= '@_out_buf' @@ -118,11 +118,11 @@ end end module InstanceMethods # Render the given template. See Render for details. - def render(template, opts = {}, &block) + def render(template, opts = OPTS, &block) if template.is_a?(Hash) if opts.empty? opts = template else opts = opts.merge(template) @@ -141,22 +141,24 @@ end end cached_template(path) do template_class.new(path, 1, render_opts[:opts].merge(opts), &template_block) - end.render(self, opts[:locals], &block) + end.render(self, (opts[:locals]||OPTS), &block) end - # Return the render options for the instance's class. + # Return the render options for the instance's class. While this + # is not currently frozen, it may be frozen in a future version, + # so you should not attempt to modify it. def render_opts self.class.render_opts end # Render the given template. If there is a default layout # for the class, take the result of the template rendering # and render it inside the layout. See Render for details. - def view(template, opts={}) + def view(template, opts=OPTS) if template.is_a?(Hash) if opts.empty? opts = template else opts = opts.merge(template) @@ -168,10 +170,10 @@ if layout = opts.fetch(:layout, render_opts[:layout]) if layout_opts = opts[:layout_opts] layout_opts = render_opts[:layout_opts].merge(layout_opts) end - content = render(layout, layout_opts||{}){content} + content = render(layout, layout_opts||OPTS){content} end content end