lib/roda/plugins/render.rb in roda-2.0.0 vs lib/roda/plugins/render.rb in roda-2.1.0

- old
+ new

@@ -36,12 +36,12 @@ # :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. # :template_opts :: The tilt options used when rendering templates, defaults to # <tt>{:outvar=>'@_out_buf', :default_encoding=>Encoding.default_external}</tt>. - # :views :: The directory holding the view files, defaults to 'views' in the - # current directory. + # :views :: The directory holding the view files, defaults to the 'views' subdirectory of the + # application's :root option (the process's working directory by default). # # Most of these options can be overridden at runtime by passing options # to the +view+ or +render+ methods: # # view('foo', :ext=>'html.erb') @@ -84,24 +84,24 @@ end end # Setup default rendering options. See Render for details. def self.configure(app, opts=OPTS) - orig_opts = opts if app.opts[:render] - app.opts[:render] = app.opts[:render].merge(opts) - else - app.opts[:render] = opts.dup + opts = app.opts[:render][:orig_opts].merge(opts) end + app.opts[:render] = opts.dup + app.opts[:render][:orig_opts] = opts opts = app.opts[:render] opts[:engine] ||= "erb" opts[:ext] = nil unless opts.has_key?(:ext) - opts[:views] ||= File.expand_path("views", Dir.pwd) + opts[:views] = File.expand_path(opts[:views]||"views", app.opts[:root]) opts[:layout_opts] = (opts[:layout_opts] || {}).dup + opts[:layout_opts][:_is_layout] = true - if layout = orig_opts.fetch(:layout, true) + if layout = opts.fetch(:layout, true) opts[:layout] = true unless opts.has_key?(:layout) case layout when Hash opts[:layout_opts].merge!(layout) @@ -194,12 +194,12 @@ else yield end end - # Given the template name and options, return the template class, template path/content, - # and template block to use for the render. + # Given the template name and options, set the template class, template path/content, + # template block, and locals to use for the render in the passed options. def find_template(opts) if content = opts[:inline] path = opts[:path] = content template_class = opts[:template_class] ||= ::Tilt[opts[:engine] || render_opts[:engine]] opts[:template_block] = Proc.new{content} @@ -219,19 +219,33 @@ path end opts[:key] = key end + if !opts[:_is_layout] && (r_locals = render_opts[:locals]) + opts[:locals] = if locals = opts[:locals] + r_locals.merge(locals) + else + r_locals + end + end + opts end # Return a single hash combining the template and opts arguments. def parse_template_opts(template, opts) template = {:template=>template} unless template.is_a?(Hash) opts.merge(template) end + # The default render options to use. These set defaults that can be overridden by + # providing a :layout_opts option to the view/render method. + def render_layout_opts + render_opts[:layout_opts].dup + end + # The name to use for the template. By default, just converts the :template option to a string. def template_name(opts) opts[:template].to_s end @@ -244,13 +258,18 @@ # If a layout should be used, return a hash of options for # rendering the layout template. If a layout should not be # used, return nil. def view_layout_opts(opts) if layout = opts.fetch(:layout, render_opts[:layout]) - layout_opts = if opts[:layout_opts] - opts[:layout_opts].merge(render_opts[:layout_opts]) - else - render_opts[:layout_opts].dup + layout_opts = render_layout_opts + if l_opts = opts[:layout_opts] + if (l_locals = l_opts[:locals]) && (layout_locals = layout_opts[:locals]) + set_locals = layout_locals.merge(l_locals) + end + layout_opts.merge!(l_opts) + if set_locals + layout_opts[:locals] = set_locals + end end case layout when Hash layout_opts.merge!(layout)