Class: Sinatra::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/template_cache.rb

Overview

Caches Haml templates in Class instance “render_haml_#template” methods, avoiding reparsing and recompiling of the template on every page reload.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_cache!Object



23
24
25
# File 'lib/sinatra/template_cache.rb', line 23

def self.clear_cache!
  instance_methods.grep(/^render_haml_/).each{|m| remove_method m}
end

Instance Method Details

#clear_cache!Object



26
# File 'lib/sinatra/template_cache.rb', line 26

def clear_cache!; self.class.clear_cache! end

#haml(template, options = {}, locals = {}) ⇒ Object

Override function responsible for calling internal Haml rendering routines



8
9
10
11
12
13
14
15
16
17
# File 'lib/sinatra/template_cache.rb', line 8

def haml(template, options={}, locals={})
  method = "render_haml_#{template}".to_sym
  return super(template, options, locals) unless self.respond_to? method
  locals = options.delete(:locals) || locals || {}
  if options[:layout] != false
    __send__(:render_haml_layout, locals) { __send__(method, locals) }
  else
    __send__(method, locals)
  end
end

#render_haml(template, data, options, locals, &block) ⇒ Object



18
19
20
21
22
# File 'lib/sinatra/template_cache.rb', line 18

def render_haml(template, data, options, locals, &block)
  method = "render_haml_#{template}".to_sym
  ::Haml::Engine.new(data, options).def_method(self.class, method, *(locals.keys))
  __send__(method, locals, &block)
end