Class: Antlers::ForNode

Inherits:
BranchNode show all
Includes:
Props, Variables
Defined in:
lib/nodes/for_node.rb

Instance Attribute Summary collapse

Attributes included from Props

#props

Attributes inherited from AntlerNode

#name

Instance Method Summary collapse

Methods included from Variables

#evaluate, #fallback

Methods included from Queries

user_defined_string?, wrapped_in?

Methods inherited from AntlerNode

#==, #eql?, #hash

Constructor Details

#initialize(name:, items:, value:, key: nil, props: [], children: []) ⇒ ForNode

Returns a new instance of ForNode.



14
15
16
17
18
19
20
# File 'lib/nodes/for_node.rb', line 14

def initialize(name:, items:, value:, key: nil, props: [], children: [])
  super(name:, props:, children:)

  @items = items
  @value = value
  @key = key
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



12
13
14
# File 'lib/nodes/for_node.rb', line 12

def children
  @children
end

Instance Method Details

#render(current_binding: nil, parent_binding: nil, slot_node: nil, namespace: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nodes/for_node.rb', line 22

def render(current_binding: nil, parent_binding: nil, slot_node: nil, namespace: nil)
  output = ''

  evaluate(name: @items, current_binding:).each do |value|
    key, value = value if @key

    # TODO: Parallelize by creating new bindings and ensuring children have any args they need via RenderEvent.
    current_binding.local_variable_set(@value, value)
    current_binding.local_variable_set(@key, key) if @key

    @children.each do |child|
      # Antlers nodes respond to "render", whereas HTML is stored as a string and output as is.
      output += (child.respond_to?(:render) ? child.render(current_binding:, parent_binding:, slot_node:, namespace:) : child) || ''
    end
  end

  output
end