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_variable

Methods inherited from AntlerNode

#==, #eql?, #hash

Constructor Details

#initialize(name:, item:, items:, props: [], children: []) ⇒ ForNode

Returns a new instance of ForNode.



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

def initialize(name:, item:, items:, props: [], children: [])
  super(name:, props:, children:)

  @item = item
  @items = items
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



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

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

  evaluate_variable(name: @items, current_binding:).each do |item|
    current_binding.local_variable_set(@item, item)

    @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