Class: Curly::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/curly/template_handler.rb

Class Method Summary collapse

Class Method Details

.call(template) ⇒ Object

Handles a Curly template, compiling it to Ruby code. The code will be evaluated in the context of an ActionView::Base instance, having access to a number of variables.

template - The ActionView::Template template that should be compiled.

Returns a String containing the Ruby code representing the template.



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
# File 'lib/curly/template_handler.rb', line 14

def self.call(template)
  path = template.virtual_path
  presenter_class = Curly::Presenter.presenter_name_for_path(path)

  source = Curly.compile(template.source)
  template_digest = Digest::MD5.hexdigest(template.source)

  # Template is empty, so there's no need to initialize a presenter.
  return %("") if template.source.empty?

  <<-RUBY
  if local_assigns.empty?
    options = assigns
  else
    options = local_assigns
  end

  presenter = #{presenter_class}.new(self, options.with_indifferent_access)

  view_function = lambda do
    #{source}
  end

  if key = presenter.cache_key
    @output_buffer = ActiveSupport::SafeBuffer.new

    template_digest = #{template_digest.inspect}

    if #{presenter_class}.respond_to?(:cache_key)
      presenter_key = #{presenter_class}.cache_key
    else
      presenter_key = nil
    end

    options = {
      expires_in: presenter.cache_duration
    }

    cache([template_digest, key, presenter_key].compact, options) do
      safe_concat(view_function.call)
    end

    @output_buffer
  else
    view_function.call.html_safe
  end
  RUBY
end