class Jeanine::Renderer

Public Class Methods

_render_with_renderer_method_name(key) click to toggle source
# File lib/jeanine/rendering.rb, line 26
def self._render_with_renderer_method_name(key)
  "_render_with_renderer_#{key}"
end
_renderers() click to toggle source
# File lib/jeanine/rendering.rb, line 13
def self._renderers
  @_renderers ||= Set.new
end
add(key, &block) click to toggle source
# File lib/jeanine/rendering.rb, line 17
def self.add(key, &block)
  define_method(_render_with_renderer_method_name(key), &block)
  _renderers << key.to_sym
end
new(response) click to toggle source
# File lib/jeanine/rendering.rb, line 30
def initialize(response)
  @response = response
end

Public Instance Methods

_render_with_renderer_method_name(key) click to toggle source
# File lib/jeanine/rendering.rb, line 22
def _render_with_renderer_method_name(key)
  self.class._render_with_renderer_method_name(key)
end
render(*args) click to toggle source
# File lib/jeanine/rendering.rb, line 34
def render(*args)
  options = _normalize_render(*args)
  _render_to_body_with_renderer(options)
end

Private Instance Methods

_normalize_args(action = nil, options = {}) click to toggle source
# File lib/jeanine/rendering.rb, line 47
def _normalize_args(action = nil, options = {})
  if action.is_a?(Hash)
    action
  else
    options
  end
end
_normalize_options(options) click to toggle source
# File lib/jeanine/rendering.rb, line 55
def _normalize_options(options)
  options
end
_normalize_render(*args, &block) click to toggle source
# File lib/jeanine/rendering.rb, line 41
def _normalize_render(*args, &block)
  options = _normalize_args(*args, &block)
  _normalize_options(options)
  options
end
_process_options(options) click to toggle source
# File lib/jeanine/rendering.rb, line 70
def _process_options(options)
  status, content_type, location = options.values_at(:status, :content_type, :location)
  @response.status = status if status
  @response.content_type = content_type if content_type
  @response.headers["Location"] = location if location
end
_render_to_body_with_renderer(options) click to toggle source
# File lib/jeanine/rendering.rb, line 59
def _render_to_body_with_renderer(options)
  self.class._renderers.each do |name|
    if options.key?(name)
      _process_options(options)
      method_name = _render_with_renderer_method_name(name)
      return send(method_name, options.delete(name), options)
    end
  end
  nil
end
cache() click to toggle source
# File lib/jeanine/rendering.rb, line 88
def cache
  Thread.current[:tilt_cache] ||= Tilt::Cache.new
end
compile_template(engine, template_name, options) click to toggle source
# File lib/jeanine/rendering.rb, line 122
def compile_template(engine, template_name, options)
  Tilt::Cache.new.fetch engine, template_name, options do
    template = Tilt[engine]
    raise "Template engine not found: #{engine}" if template.nil?
    template_name = find_template!(template_name)
    template.new(template_name, options)
  end
end
find_template!(template) click to toggle source
# File lib/jeanine/rendering.rb, line 92
def find_template!(template)
  Jeanine.view_paths.to_a.each do |path|
    template_with_path = "#{path}/#{template}"
    if File.exist?(template_with_path)
      return template_with_path
    end
  end
  raise "Template not found in view paths. Looking for #{template} in #{Jeanine.view_paths.to_a.join(', ')}"
end
html(engine, template_name, options = {}, locals = {}, &block) click to toggle source
# File lib/jeanine/rendering.rb, line 102
def html(engine, template_name, options = {}, locals = {}, &block)
  locals          = options.delete(:locals) || locals || {}
  layout          = options[:layout]
  scope           = options.delete(:scope) || self
  options.delete(:layout)
  options[:outvar] ||= '@_out_buf'
  options[:default_encoding] ||= "UTF-8"
  template        = compile_template(engine, template_name, options)
  output          = template.render(scope, locals, &block)
  if layout
    unless layout.include?("layouts/")
      layout = "layouts/#{layout}"
    end
    layout = find_template!(layout)
    options = options.merge(layout: false, scope: scope)
    catch(:layout_missing) { return html(engine, layout, options, locals) { output } }
  end
  output
end