Class: KitLiquidRails::TemplateHandler

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ TemplateHandler

Returns a new instance of TemplateHandler.



11
12
13
# File 'lib/kit_liquid_rails/template_handler.rb', line 11

def initialize(context)
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/kit_liquid_rails/template_handler.rb', line 3

def context
  @context
end

Class Method Details

.call(template, source) ⇒ Object

Note: local_assigns is not available in ViewComponents



6
7
8
9
# File 'lib/kit_liquid_rails/template_handler.rb', line 6

def self.call(template, source)
  source ||= template.source
  "::KitLiquidRails::TemplateHandler.new(self).render(#{ source.inspect }, (defined?(local_assigns) ? local_assigns : nil))"
end

Instance Method Details

#compilable?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/kit_liquid_rails/template_handler.rb', line 29

def compilable?
  false
end

#render(source, local_assigns = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kit_liquid_rails/template_handler.rb', line 15

def render(source, local_assigns = nil)
  local_assigns ||= {}

  # Use `liquid_assigns` method on the `ViewComponent` to determine what to forward to Liquid.
  if context.is_a?(::ViewComponent::Base) && context.respond_to?(:liquid_assigns)
    local_assigns = context.liquid_assigns
  end

  local_assigns = local_assigns&.deep_stringify_keys

  liquid = Liquid::Template.parse(source)
  liquid.render(local_assigns).html_safe
end