Module: Sinatra::Templates

Defined in:
lib/sinatra/templates.rb

Instance Method Summary collapse

Instance Method Details

#cache_output(key, content, opts) ⇒ Object

Cache output



15
16
17
18
19
# File 'lib/sinatra/templates.rb', line 15

def cache_output(key, content, opts)
  content = minify_output(content) if settings.cache_minify_pages && opts[:minify]
  expires = opts[:expires] ? opts[:expires] : settings.cache_expires
  output  = cache_set(key, content, expires)
end

#get_setting(key, default) ⇒ Object

DRY



5
6
7
# File 'lib/sinatra/templates.rb', line 5

def get_setting(key, default)
  settings.respond_to?(key) ? settings.send(key) : default
end

#minify_output(content) ⇒ Object

Minification



10
11
12
# File 'lib/sinatra/templates.rb', line 10

def minify_output(content)
  content.gsub(/^\s*\/\/.+$|<!--(.*?)-->|\t|\n|\r/, '')
end

#render(engine, data, options = {}, locals = {}, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sinatra/templates.rb', line 3

def render(engine, data, options={}, locals={}, &block)
  # DRY
  def get_setting(key, default)
    settings.respond_to?(key) ? settings.send(key) : default
  end

  # Minification
  def minify_output(content)
    content.gsub(/^\s*\/\/.+$|<!--(.*?)-->|\t|\n|\r/, '')
  end

  # Cache output
  def cache_output(key, content, opts)
    content = minify_output(content) if settings.cache_minify_pages && opts[:minify]
    expires = opts[:expires] ? opts[:expires] : settings.cache_expires
    output  = cache_set(key, content, expires)
  end

  # merge app-level options
  engine_options  = get_setting(engine, {})
  options         = engine_options.merge(options)

  # extract generic options
  locals          = options.delete(:locals) || locals         || {}
  views           = options.delete(:views)  || settings.views || "./views"
  layout          = options.delete(:layout)
  eat_errors      = layout.nil?
  layout          = engine_options[:layout] if layout.nil? or layout == true
  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

  # set some defaults
  options[:outvar]           ||= '@_out_buf'
  options[:default_encoding] ||= settings.default_encoding

  # Cache defaults
  options =
  {
    cache: true,
    minify: true,
    expires: false
  }.merge!(options)

  sd_do_cache = settings.cache_enabled && (settings.environment == settings.cache_environment)
  sd_do_cache = sd_do_cache && settings.cache_pages && options[:cache]
  sd_page_key = "_sinatralli_page_#{request.host + request.path_info.gsub(/^\//, '')}_#{data.to_s}"

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

  # render layout
  if layout
    # Fetch the copy
    if sd_do_cache
      cached = cache_get(sd_page_key)
      return cached unless cached.nil?
    end
    options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope)
    catch(:layout_missing) {
      output = render(layout_engine, layout, options, locals) { output }
      return sd_do_cache ? cache_output(sd_page_key, output, options) : output
    }
  end

  output.extend(ContentTyped).content_type = content_type if content_type
  sd_do_cache ? cache_output(sd_page_key, output, options) : output
end