Class: Liquid::Rails::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid4-rails/template_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ TemplateHandler

Returns a new instance of TemplateHandler.



9
10
11
12
13
# File 'lib/liquid4-rails/template_handler.rb', line 9

def initialize(view)
  @view       = view
  @controller = @view.controller
  @helper     = ActionController::Base.helpers
end

Class Method Details

.call(template) ⇒ Object



5
6
7
# File 'lib/liquid4-rails/template_handler.rb', line 5

def self.call(template)
  "Liquid::Rails::TemplateHandler.new(self).render(#{template.source.inspect}, local_assigns)"
end

Instance Method Details

#compilable?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/liquid4-rails/template_handler.rb', line 44

def compilable?
  false
end

#filtersObject



36
37
38
39
40
41
42
# File 'lib/liquid4-rails/template_handler.rb', line 36

def filters
  if @controller.respond_to?(:liquid_filters, true)
    @controller.send(:liquid_filters)
  else
    [@controller._helpers]
  end
end

#render(template, local_assigns = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/liquid4-rails/template_handler.rb', line 15

def render(template, local_assigns={})
  @view.controller.headers['Content-Type'] ||= 'text/html; charset=utf-8'

  assigns = if @controller.respond_to?(:liquid_assigns, true)
    @controller.send(:liquid_assigns)
  else
    @view.assigns
  end
  assigns['content_for_layout'] = @view.content_for(:layout) if @view.content_for?(:layout)
  assigns.merge!(local_assigns.stringify_keys)

  debug_mode = ::Rails.env.development? || ::Rails.env.test?
  if debug_mode
    Liquid.cache_classes = false
    Liquid::Template.error_mode = :strict
  end

  liquid = Liquid::Template.parse(template)
  liquid.send(debug_mode ? :render! : :render, assigns, filters: filters, registers: { view: @view, controller: @controller, helper: @helper }, strict_variables: debug_mode, strict_filters: debug_mode).html_safe
end