module Roda::RodaPlugins::Render

def self.configure(app, opts=OPTS)

Setup default rendering options. See Render for details.
def self.configure(app, opts=OPTS)
  if app.opts[:render]
    orig_cache = app.opts[:render][:cache]
    orig_method_cache = app.opts[:render][:template_method_cache]
    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] = (opts[:engine] || "erb").dup.freeze
  opts[:views] = app.expand_path(opts[:views]||"views").freeze
  opts[:allowed_paths] ||= [opts[:views]].freeze
  opts[:allowed_paths] = opts[:allowed_paths].map{|f| app.expand_path(f, nil)}.uniq.freeze
  opts[:check_paths] = true unless opts.has_key?(:check_paths)
  unless opts.has_key?(:check_template_mtime)
    opts[:check_template_mtime] = if opts[:cache] == false || opts[:explicit_cache]
      true
    else
      ENV['RACK_ENV'] == 'development'
    end
  end
  begin
    app.const_get(:RodaCompiledTemplates, false)
  rescue NameError
    compiled_templates_module = Module.new
    app.send(:include, compiled_templates_module)
    app.const_set(:RodaCompiledTemplates, compiled_templates_module)
  end
  opts[:template_method_cache] = orig_method_cache || (opts[:cache_class] || RodaCache).new
  opts[:template_method_cache][:_roda_layout] = nil if opts[:template_method_cache][:_roda_layout]
  opts[:cache] = orig_cache || (opts[:cache_class] || RodaCache).new
  opts[:layout_opts] = (opts[:layout_opts] || {}).dup
  opts[:layout_opts][:_is_layout] = true
  if opts[:layout_opts][:views]
    opts[:layout_opts][:views] = app.expand_path(opts[:layout_opts][:views]).freeze
  end
  if layout = opts.fetch(:layout, true)
    opts[:layout] = true
    case layout
    when Hash
      opts[:layout_opts].merge!(layout)
    when true
      opts[:layout_opts][:template] ||= 'layout'
    else
      opts[:layout_opts][:template] = layout
    end
    opts[:optimize_layout] = (opts[:layout_opts][:template] if opts[:layout_opts].keys.sort == [:_is_layout, :template])
  end
  opts[:layout_opts].freeze
  template_opts = opts[:template_opts] = (opts[:template_opts] || {}).dup
  template_opts[:outvar] ||= '@_out_buf'
  unless template_opts.has_key?(:default_encoding)
    template_opts[:default_encoding] = Encoding.default_external
  end
  engine_opts = opts[:engine_opts] = (opts[:engine_opts] || {}).dup
  engine_opts.to_a.each do |k,v|
    engine_opts[k] = v.dup.freeze
  end
  if escape = opts[:escape]
    require 'tilt/erubi'
    case escape
    when String, Array
      Array(escape).each do |engine|
        engine_opts[engine] = (engine_opts[engine] || {}).merge(:escape => true).freeze
      end
    else
      template_opts[:escape] = true
    end
  end
  template_opts.freeze
  engine_opts.freeze
  opts.freeze
end