module Sinatra::Templates

Public Instance Methods

render(engine, data, options={}, locals={}, &block) click to toggle source
# File lib/sinatra_fedora/helpers.rb, line 44
def render(engine, data, options={}, locals={}, &block)
  # Merge app-level options
  options = settings.send(engine).merge(options) if settings.respond_to?(engine)
  options[:outvar]           ||= '@_out_buf'
  options[:default_encoding] ||= settings.default_encoding

  # Extract generic options
  locals          = options.delete(:locals) || locals         || {}
  views           = options.delete(:views)  || settings.views || "./views"
  @default_layout = :layout if @default_layout.nil?
  layout          = options.delete(:layout)
  eat_errors      = layout.nil?
  layout          = @default_layout if layout.nil? or layout == true
  content_type    = options.delete(:content_type)  || options.delete(:default_content_type)
  layout_engine   = options.delete(:layout_engine) || engine
  scope           = options.delete(:scope)         || self

  # Fedora views
  if data != :layout and options[:views_directory].nil?
    look_in = (scope.class.views_from || scope.class.name.downcase)
    data = "#{look_in}/#{data.to_s}".to_sym
  end

  # Compile and render template
  layout_was      = @default_layout
  @default_layout = false
  template        = compile_template(engine, data, options, views)
  output          = template.render(scope, locals, &block)
  @default_layout = layout_was

  # Render layout
  if layout
    options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope)
    catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } }
  end

  output.extend(ContentTyped).content_type = content_type if content_type
  output
end