Class: Bridgetown::Tags::RubyRender

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
bridgetown-core/lib/bridgetown-core/tags/ruby_render.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, input, tokens) ⇒ RubyRender

Returns a new instance of RubyRender.

Parameters:

  • tag_name (String)

    “ruby_render”

  • input (String)

    The input to the tag: snake-case name of the Ruby component, plus initialize args. Example: ‘“card”, title: “Hello”, footer: “I am a card”’

  • tokens (Hash)

    A hash of config tokens for Liquid



13
14
15
16
17
18
19
20
21
# File 'bridgetown-core/lib/bridgetown-core/tags/ruby_render.rb', line 13

def initialize(tag_name, input, tokens)
  super
  @input = input

  @attributes = {}
  input.scan(Liquid::TagAttributes) do |key, value|
    @attributes[key.to_sym] = parse_expression(value)
  end
end

Instance Method Details

#render(context) ⇒ String

Parameters:

  • context (Liquid::Context)

Returns:

  • (String)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'bridgetown-core/lib/bridgetown-core/tags/ruby_render.rb', line 25

def render(context)
  view_context = Bridgetown::RubyTemplateView.new(context.registers[:resource])
  component_class_name_snakecase = @input.split(",").first
  component_class_name = component_class_name_snakecase.tr("\"'", "").camelize.strip
  component_class = self.class.const_get(component_class_name)
  @attributes.each do |key, value|
    @attributes[key] = value.evaluate(context) if value.is_a?(Liquid::VariableLookup)
  end
  component_instance = component_class.new(**@attributes)

  if component_instance.respond_to?(:render_in)
    component_instance.render_in(view_context)
  else
    component_instance.to_s
  end
end